Google の Web Fonts API は、フォントの読み込みが完了した場合、または読み込めなかった場合などに実行されるコールバック関数を定義する方法を提供します。CSS3 Web フォント (@font-face) を使用して同様のことを達成する方法はありますか?
5 に答える
2015年アップデート
Chrome 35 以降と Firefox 41 以降は、CSS フォント読み込み API ( MDN、W3C ) を実装しています。を呼び出しdocument.fonts
てFontFaceSetオブジェクトを取得します。このオブジェクトには、フォントの読み込み状態を検出するための便利な API がいくつかあります。
check(fontSpec)
- 指定されたフォント リスト内のすべてのフォントが読み込まれて使用可能かどうかを返します。は、フォントfontSpec
の CSS 短縮構文を使用します。例:document.fonts.check('bold 16px Roboto'); // true or false
document.fonts.ready
-フォントの読み込みとレイアウト操作が完了したことを示すPromiseを返します。
例:document.fonts.ready.then(function () { /*... all fonts loaded...*/ });
以下は、これらの 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>
IE 11 は API をサポートしていません。IE をサポートする必要がある場合は、利用可能なポリフィルまたはサポート ライブラリを確認してください。
- Web フォント ローダー- Google と Adobe が開発
- FontFaceOnload - Web フォント ローダーと同様のより軽量なアプローチ
- FontLoader - ポリフィル
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。
それはあなたが尋ねるもののためのコールバックを定義します.
window.load イベントは、すべてがロードされたときに発生します。これにはフォントが含まれている必要があります。したがって、それをコールバックとして使用できます。ただし、Web フォント ローダーを
google、typekit、ascender、および monotype オプションに加えて、任意の Web フォント プロバイダーからスタイルシートをロードできるカスタム モジュールもあります。
WebFontConfig = { custom: { 家族: ['OneFont', 'AnotherFont'], urls: [ 'http://myotherwebfontprovider.com/stylesheet1.css', 'http://yetanotherwebfontprovider.com/stylesheet2.css' ] } };
ライブラリは、指定したプロバイダーに関係なく、同じイベントを送信します。