Dynamically load google fonts after page has loaded

后端 未结 2 1830
一整个雨季
一整个雨季 2021-02-05 04:29

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

2条回答
  •  情深已故
    2021-02-05 05:05

    Or if you don't want third party libs :

    function addStylesheetURL(url) {
      var link = document.createElement('link');
      link.rel = 'stylesheet';
      link.href = url;
      document.getElementsByTagName('head')[0].appendChild(link);
    }
    
    // Load Tangerine & Cantarell
    addStylesheetURL('https://fonts.googleapis.com/css2?family=Cantarell&family=Tangerine&display=swap');
        h1 {
          font-family: 'Cantarell', sans-serif;
        }
        p {
          font-family: 'Tangerine', cursive;
          font-size: 30px;
        }
    
    
      
        Dynamically load google fonts after page has loaded
        
      
      
        

    Dynamically load google fonts after page has loaded

    Some text.

提交回复
热议问题