加载Web字体后如何通知

| Google的Web字体API提供了一种方法,用于定义在字体已完成加载或无法加载等情况下执行的回调函数。是否可以使用CSS3网络字体(@ font-face )?     
已邀请:
2015年更新 Chrome 35+和Firefox 41+实现了CSS字体加载API(MDN,W3C)。调用0来获取FontFaceSet对象,该对象具有一些用于检测字体的加载状态的有用API:
check(fontSpec)
-返回给定字体列表中的所有字体是否已加载并可用。
fontSpec
使用CSS速记语法表示字体,例如:
document.fonts.check(\'bold 16px Roboto\');  // true or false
document.fonts.ready
-返回一个Promise,指示完成字体加载和布局操作。例如::5ѭ 这是显示这些API的代码片段以及
document.fonts.onloadingdone
,其中提供了有关字体外观的更多信息。
alert(\'Roboto loaded? \' + document.fonts.check(\'1em Roboto\'));  // false

document.fonts.ready.then(function () {
  alert(\'All fonts in use by visible text have loaded.\');
   alert(\'Roboto loaded? \' + document.fonts.check(\'1em Roboto\'));  // true
});

document.fonts.onloadingdone = function (fontFaceSetEvent) {
   alert(\'onloadingdone we have \' + fontFaceSetEvent.fontfaces.length + \' font faces loaded\');
};
<link href=\'https://fonts.googleapis.com/css?family=Roboto:400,700\' rel=\'stylesheet\' type=\'text/css\'>
<p style=\"font-family: Roboto\">
  We need some text using the font, for the font to be loaded.
  So far one font face was loaded.
  Let\'s add some <strong>strong</strong> text to trigger loading the second one,
    with weight: 700.
</p>
在Safari,Chrome,Firefox,Opera,IE7,IE8,IE9中进行了测试:
function waitForWebfonts(fonts, callback) {
    var loadedFonts = 0;
    for(var i = 0, l = fonts.length; i < l; ++i) {
        (function(font) {
            var node = document.createElement(\'span\');
            // Characters that vary significantly among different fonts
            node.innerHTML = \'giItT1WQy@!-/#\';
            // Visible - so we can measure it - but not on the screen
            node.style.position      = \'absolute\';
            node.style.left          = \'-10000px\';
            node.style.top           = \'-10000px\';
            // Large font size makes even subtle changes obvious
            node.style.fontSize      = \'300px\';
            // Reset any font properties
            node.style.fontFamily    = \'sans-serif\';
            node.style.fontVariant   = \'normal\';
            node.style.fontStyle     = \'normal\';
            node.style.fontWeight    = \'normal\';
            node.style.letterSpacing = \'0\';
            document.body.appendChild(node);

            // Remember width with no applied web font
            var width = node.offsetWidth;

            node.style.fontFamily = font;

            var interval;
            function checkFont() {
                // Compare current width with original width
                if(node && node.offsetWidth != width) {
                    ++loadedFonts;
                    node.parentNode.removeChild(node);
                    node = null;
                }

                // If all fonts have been loaded
                if(loadedFonts >= fonts.length) {
                    if(interval) {
                        clearInterval(interval);
                    }
                    if(loadedFonts == fonts.length) {
                        callback();
                        return true;
                    }
                }
            };

            if(!checkFont()) {
                interval = setInterval(checkFont, 50);
            }
        })(fonts[i]);
    }
};
像这样使用它:
waitForWebfonts([\'MyFont1\', \'MyFont2\'], function() {
    // Will be called as soon as ALL specified fonts are available
});
    
无需服务即可使用Google Web Fonts API(和Typekit)使用的JS库:WebFont Loader。 它为您的要求定义了回调,以及更多。     
2017更新 截至2017年,JS库FontFaceObserver绝对是最好,最轻便的跨浏览器解决方案。它还公开了基于Promise的
.load()
界面。     
加载完所有内容后,将触发window.load事件-该事件应包含字体 因此,您可以将其用作回调。但是我不认为您必须决定使用网络字体加载器作为   除了Google,Typekit,   升序和单型选项   也是可以加载的自定义模块   任何网络字体的样式表   提供者。      WebFontConfig = {自定义:{   家庭:[\'OneFont \',\'AnotherFont \'],       网址:[\'http://myotherwebfontprovider.com/stylesheet1.css \',         \'http://yetanotherwebfontprovider.com/stylesheet2.css \'   ]}}; 无论您指定哪个提供程序,库都会发送相同的事件。     

要回复问题请先登录注册