此解决方案使用两个样式表:wpse-4508-default.css
和wpse-4508-small.css
. 我们总是加载默认字体,如果某个字体不存在,也会加载小版本。我们将结果保存在cookie中,以便下次不需要在客户端进行测试和额外加载,从而获得更流畅的体验。代码设计为从主题目录运行,因此您可以将PHP代码添加到functions.php
. 我加了一些var_dump()
和alert()
帮助您了解发生了什么。我只在Mac OS X上的Safari中进行了测试。
<?php
add_action( \'wp_head\', \'wpse4508_wp_head\' );
function wpse4508_wp_head()
{
// Always display our normal stylesheet
wp_enqueue_style( \'wpse4508_default\', get_stylesheet_directory_uri() . \'/wpse-4508-default.css\' );
$urlSmallStylesheet = get_stylesheet_directory_uri() . \'/wpse-4508-small.css\';
if ( ! array_key_exists( \'wpse4508_hasfont\', $_COOKIE ) ) {
// We don\'t know anything, detect it
var_dump( \'No cookie info, loading JS\' );
wp_enqueue_script( \'wpse4508_detector\', get_stylesheet_directory_uri() . \'/wpse-4508-detector.js\', array(), false, true );
// Send the style sheet URL to the front
wp_localize_script( \'wpse4508_detector\', \'WPSE4508_Detector\', array( \'url\' => $urlSmallStylesheet ));
} elseif ( \'false\' == $_COOKIE[\'wpse4508_hasfont\'] ) {
// We know we should load the small stylesheet
var_dump( \'Cookie info, no font, extra stylesheet via PHP\' );
wp_enqueue_style( \'wpse4508_small\', $urlSmallStylesheet );
}
}
// A simple div to test this code
add_action( \'wp_footer\', \'wpse4508_wp_footer\' );
function wpse4508_wp_footer()
{
echo \'<div class="wpse4508_test">WPSE 4508 test</div>\';
}
The
wpse-4508-default.css
文件:
.wpse4508_test
{
background-color: red;
font-size: 20px;
width: 100%;
text-align: center;
position: absolute;
top: 0;
}
The
wpse-4508-small.css
, 有额外的规则。我不认为
!important
是必需的,因为它们是在前一个之后加载的,请自己尝试。
.wpse4508_test
{
background-color: green !important;
font-size: 10px !important;
}
检测字体并设置cookie的Javascript文件。
var Detector = function(){
var h = document.getElementsByTagName("BODY")[0];
var d = document.createElement("DIV");
var s = document.createElement("SPAN");
d.appendChild(s);
d.style.fontFamily = "sans"; //font for the parent element DIV.
s.style.fontFamily = "sans"; //serif font used as a comparator.
s.style.fontSize = "72px"; //we test using 72px font size, we may use any size. I guess larger the better.
s.innerHTML = "mmmmmmmmmmlil"; //we use m or w because these two characters take up the maximum width. And we use a L so that the same matching fonts can get separated
h.appendChild(d);
var defaultWidth = s.offsetWidth; //now we have the defaultWidth
var defaultHeight = s.offsetHeight; //and the defaultHeight, we compare other fonts with these.
h.removeChild(d);
/* test
* params:
* font - name of the font you wish to detect
* return:
* f[0] - Input font name.
* f[1] - Computed width.
* f[2] - Computed height.
* f[3] - Detected? (true/false).
*/
function debug(font) {
h.appendChild(d);
var f = [];
f[0] = s.style.fontFamily = font; // Name of the font
f[1] = s.offsetWidth; // Width
f[2] = s.offsetHeight; // Height
h.removeChild(d);
font = font.toLowerCase();
if (font == "serif") {
f[3] = true; // to set arial and sans-serif true
} else {
f[3] = (f[1] != defaultWidth || f[2] != defaultHeight); // Detected?
}
return f;
}
function test(font){
f = debug(font);
return f[3];
}
this.detailedTest = debug;
this.test = test;
}
WPSE4508_Detector.loadStylesheetIfNeeded = function() {
var hasFont = new Detector().test(\'Andalus\');
alert( \'Testing for font: \' + hasFont );
document.cookie = \'wpse4508_hasfont=\' + (hasFont ? \'true\' : \'false\'); // Maybe add expiry date?
if (!hasFont) {
alert( \'Loading stylesheet via JS\' );
var elHead = document.getElementsByTagName(\'head\')[0];
var elLink = document.createElement(\'link\');
elLink.rel = \'stylesheet\';
elLink.type = \'text/css\';
elLink.href = WPSE4508_Detector.url;
elHead.appendChild(elLink);
}
}
WPSE4508_Detector.loadStylesheetIfNeeded();