javascript 页面加载后动态加载谷歌字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16553326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:06:18  来源:igfitidea点击:

Dynamically load google fonts after page has loaded

javascriptjquerycssfont-facegoogle-font-api

提问by Alexis

I would like to be able to have the user select which font they would like the page to be displayed in. Hereis the way that Google recommends you do it using JavaScript.

我希望能够让用户选择他们希望页面显示的字体。 是 Google 建议您使用 JavaScript 的方式。

WebFontConfig = {
    google: {
        families: ['Tangerine', 'Cantarell']
    }
};

(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);
      })();

How can I modify this so that I can re-get fonts after the page has loaded?

如何修改它以便在页面加载后重新获取字体?

回答by mccannf

Check out the WebFont.load command in this github repo:

查看此 github 存储库中的 WebFont.load 命令:

https://github.com/typekit/webfontloader

https://github.com/typekit/webfontloader

You can load whatever font you want dynamically:

你可以动态加载任何你想要的字体:

 <script src="http://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js"></script> 
  <script> 
        WebFont.load({
                    google: { 
                           families: ['Droid Sans', 'Droid Serif'] 
                     } 
         }); 
   </script>