网络字体加载后如何收到通知
Google 的 Web Fonts API 提供了一种方法来定义在字体已完成加载或无法加载等情况下要执行的回调函数。有没有办法使用 CSS3 Web 字体(@font-face)实现类似的功能?
Google's Web Fonts API offers a way to define callback functions to be executed if a font has finished loading, or couldn't be loaded etc. Is there a way to achieve something similar using CSS3 web fonts (@font-face)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
2015 更新
Chrome 35+ 和 Firefox 41+ 实现 CSS 字体加载 API (MDN,W3C)。调用
document.fonts
获取 FontFaceSet 对象,它有一些有用的 API 用于检测字体的加载状态:check(fontSpec)
- 返回给定字体列表中的所有字体是否已加载且可用。fontSpec
使用 CSS 字体速记语法.示例:
document.fonts.check('bold 16px Roboto'); // true 或 false
document.fonts .ready
- 返回 Promise 表示字体加载和布局操作已完成。示例:
document.fonts.ready.then(function () { /*...所有字体已加载。 ..*/ });
这是显示这些 API 的代码片段,以及提供有关字体的额外信息的
document.fonts.onloadingdone
。IE 11 不支持该 API。如果您需要支持 IE,请查看可用的 polyfill 或支持库:
2015 Update
Chrome 35+ and Firefox 41+ implement the CSS font loading API (MDN, W3C). Call
document.fonts
to get a FontFaceSet object, which has a few useful APIs for detecting the load status of fonts:check(fontSpec)
- returns whether all fonts in the given font list have been loaded and are available. ThefontSpec
uses the CSS shorthand syntax for fonts.Example:
document.fonts.check('bold 16px Roboto'); // true or false
document.fonts.ready
- returns a Promise indicating that font loading and layout operations are done.Example:
document.fonts.ready.then(function () { /*... all fonts loaded...*/ });
Here's a snippet showing these APIs, plus
document.fonts.onloadingdone
, which offers extra information about the font faces.IE 11 doesn't support the API. Look at available polyfills or support libraries if you need to support IE:
在 Safari、Chrome、Firefox、Opera、IE7、IE8、IE9 中测试:
使用如下:
Tested in Safari, Chrome, Firefox, Opera, IE7, IE8, IE9:
Use it like:
Google Web Fonts API(和 Typekit)使用的 JS 库可以在没有服务的情况下使用:WebFont Loader。
它定义了您所要求的回调,以及更多。
The JS Library used by Google Web Fonts API (and Typekit) can be used without the service: WebFont Loader.
It defines callbacks for what you ask, and many more.
2017更新
JS库FontFaceObserver绝对是最好的,最轻量级的,跨平台的截至 2017 年的浏览器解决方案。它还公开了基于 Promise 的
.load()
接口。2017 Update
The JS library FontFaceObserver is definitely the best, most lightweight, cross-browser solution as of 2017. It also exposes a Promise-based
.load()
interface.document.fonts.ready
字段在 Safari 上不可靠。我发现唯一可靠的跨浏览器方式(现代浏览器)是重复检查document.fonts.status === 'loaded'
。这是一个指数回退的示例:The
document.fonts.ready
field is unreliable on Safari. I have found the only reliable cross-browser way (modern browsers) to be repeatedly checking fordocument.fonts.status === 'loaded'
. Here's an example with exponential back off:我创建了两种方法来检查特定字体。第一种方法是最好的方法,因为它直接使用“字体”接口和“检查”方法。第二种方法虽然不太好,但仍然有效,因为它通过将默认字体的文本大小与新字体的文本大小进行比较来直接在 DOM 中检测差异。尽管很少见,但字体大小如此接近而导致事件不会触发是有可能的,但我认为这种可能性极小。如果发生这种情况,您还可以添加另一个跨度来检查衬线字体之间的差异。
(虽然它是纯 JavaScript,但它可以与 React 一起使用)
方法 1
方法 2
I created two methods to check for a specific font. The first method is the best one as it uses the 'fonts' interface directly with the 'check' method. The second method is not as good, but still functional, as it detects the difference directly in the DOM by comparing the size of the text with the default font to the text with the new font. It's possible, although rare, for the fonts to be so close in size that the event wouldn't fire, but I think it's highly unlikely. If that happens, you can add another span to check the difference between the serif font as well.
(Although it is pure javascript it works with React)
METHOD 1
METHOD 2
当所有内容都已加载时, window.load 事件将触发 - 其中应该包括字体
所以你可以用它作为回调。不过,我认为您不必这样做,因为您决定使用网络字体加载器作为
无论您指定哪个提供商,库都会发送相同的事件。
The window.load event will fire when everything has loaded - that should include fonts
So you could use that as the call back. However I don't think you have to is you decide to use the web font loader as
The library sends the same events regardless of which provider you specify.