CSS:仅在mac上使用字体

时间:2011-05-29 01:26:38

标签: css css3 font-face fallback

@ font-face非常棒,但它在PC上显得很安静。只有在Mac上(不了解linux)才能自然显示。如何仅为mac用户显示特殊字体并为PC用户回退到常规字体?

谢谢!

3 个答案:

答案 0 :(得分:4)

如果您不介意使用JavaScript和jQuery,可以使用jQuery client plugin检测操作系统,然后设置字体。它似乎是旧的东西,但它适用于jQuery 1.3.2,如果它适用于jQuery 1.6.1我不会感到惊讶。

<script src="./jquery-1.3.2.js"></script> 
<script src="./jquery.client.js"></script> 
<script type="text/javascript"> 
    if($.client.os == 'Mac')
        $('body').css('font-family',"'Looks good in Mac'")
    else if($.client.os == 'Windows')
        $('body').css('font-family',"'Looks good in Windows'")
    else if($.client.os == 'Linux')
        $('body').css('font-family',"'Looks good in Linux'")
</script> 

您可以将body替换为任何CSS选择器,即使浏览器不支持它,jQuery也可能支持它。

jQuery is available from jquery.com

完整jquery.client.js

(function() {

    var BrowserDetect = {
        init: function () {
            this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
            this.version = this.searchVersion(navigator.userAgent)
                || this.searchVersion(navigator.appVersion)
                || "an unknown version";
            this.OS = this.searchString(this.dataOS) || "an unknown OS";
        },
        searchString: function (data) {
            for (var i=0;i<data.length;i++) {
                var dataString = data[i].string;
                var dataProp = data[i].prop;
                this.versionSearchString = data[i].versionSearch || data[i].identity;
                if (dataString) {
                    if (dataString.indexOf(data[i].subString) != -1)
                        return data[i].identity;
                }
                else if (dataProp)
                    return data[i].identity;
            }
        },
        searchVersion: function (dataString) {
            var index = dataString.indexOf(this.versionSearchString);
            if (index == -1) return;
            return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
        },
        dataBrowser: [
            {
                string: navigator.userAgent,
                subString: "Chrome",
                identity: "Chrome"
            },
            {   string: navigator.userAgent,
                subString: "OmniWeb",
                versionSearch: "OmniWeb/",
                identity: "OmniWeb"
            },
            {
                string: navigator.vendor,
                subString: "Apple",
                identity: "Safari",
                versionSearch: "Version"
            },
            {
                prop: window.opera,
                identity: "Opera"
            },
            {
                string: navigator.vendor,
                subString: "iCab",
                identity: "iCab"
            },
            {
                string: navigator.vendor,
                subString: "KDE",
                identity: "Konqueror"
            },
            {
                string: navigator.userAgent,
                subString: "Firefox",
                identity: "Firefox"
            },
            {
                string: navigator.vendor,
                subString: "Camino",
                identity: "Camino"
            },
            {       // for newer Netscapes (6+)
                string: navigator.userAgent,
                subString: "Netscape",
                identity: "Netscape"
            },
            {
                string: navigator.userAgent,
                subString: "MSIE",
                identity: "Explorer",
                versionSearch: "MSIE"
            },
            {
                string: navigator.userAgent,
                subString: "Gecko",
                identity: "Mozilla",
                versionSearch: "rv"
            },
            {       // for older Netscapes (4-)
                string: navigator.userAgent,
                subString: "Mozilla",
                identity: "Netscape",
                versionSearch: "Mozilla"
            }
        ],
        dataOS : [
            {
                string: navigator.platform,
                subString: "Win",
                identity: "Windows"
            },
            {
                string: navigator.platform,
                subString: "Mac",
                identity: "Mac"
            },
            {
                string: navigator.userAgent,
                subString: "iPhone",
                identity: "iPhone/iPod"
            },
            {
                string: navigator.platform,
                subString: "Linux",
                identity: "Linux"
            }
        ]

    };

    BrowserDetect.init();

    window.$.client = { os : BrowserDetect.OS, browser : BrowserDetect.browser };

})();

答案 1 :(得分:3)

jQuery客户端插件非常酷。至少使用普通的JavaScript进行操作系统嗅探(我假设这会节省一些开销),你可以这样做:

$(document).ready( function () {
    var os = navigator.platform;
    if (os.indexOf("Mac") != -1) {
        $('body').css( 'font-family', '"Lucida Grande"' );
    }
    else if (os.indexOf("Win") != -1) {
        $('body').css( 'font-family', '"Arial"' );
    }
    else if (os.indexOf("iPhone") != -1) {
        $('body').css( 'font-family', '"Helvetica"' );
    }
});

答案 2 :(得分:2)

尽管我同意你对Windows的看法,但我担心你无法单独使用CSS来通过Windows浏览器告诉Mac浏览器。您需要使用服务器端脚本(或者JavaScript?)来嗅探它,并相应地决定是否加载@font-face CSS。