gpt4 book ai didi

javascript - 如何检测谷歌网络字体何时准备好并显示在页面中?

转载 作者:数据小太阳 更新时间:2023-10-29 04:11:57 26 4
gpt4 key购买 nike

我需要在我的页面中的字体切换为 google 字体时立即触发一个操作。(我使用的是 css 模式)字体切换时是否触发了任何 DOM 事件?

最佳答案

David Walsh 在此处提供了使用 Google Webfonts API 的指南:http://davidwalsh.name/google-fonts-api

这是他的帖子中的一个例子:

WebFontConfig = {
google: {
families: [ 'Tangerine', 'Cantarell' ]
},
/* Called when all the specified web-font provider modules (google, typekit, and/or custom) have reported that they have started loading fonts. */
loading: function() {
// do something
},
/* Called when each requested web font has started loading. The fontFamily parameter is the name of the font family, and fontDescription represents the style and weight of the font. */
fontloading: function(fontFamily, fontDescription) {
// do something
},
/* Called when each requested web font has finished loading. The fontFamily parameter is the name of the font family, and fontDescription represents the style and weight of the font. */
fontactive: function(fontFamily, fontDescription) {
// do something
},
/* Called if a requested web font failed to load. The fontFamily parameter is the name of the font family, and fontDescription represents the style and weight of the font. */
fontinactive: function(fontFamily, fontDescription) {
// do something
},
/* Called when all of the web fonts have either finished loading or failed to load, as long as at least one loaded successfully. */
active: function() {
// do something
},
/* Called if the browser does not support web fonts or if none of the fonts could be loaded. */
inactive: function() {
// do something
}
};

/* async! */
(function() {
var wf = document.createElement('script');
wf.src = ('https:' == document.location.protocol ? 'https' : 'http') + '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
wf.type = 'text/javascript';
wf.async = 'true';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(wf, s);
})();

关于javascript - 如何检测谷歌网络字体何时准备好并显示在页面中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9863931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com