Preloading @font-face fonts?

前端 未结 13 1891
时光说笑
时光说笑 2020-12-04 11:13

Is it possible to preload or otherwise cache @font-face fonts, most likely with javascript, before the page loads so you don\'t get that ugly jump when the page finally does

13条回答
  •  被撕碎了的回忆
    2020-12-04 11:28

    Your head should include the preload rel as follows:

    
        ...
        
        
    
    

    This way woff2 will be preloaded by browsers that support preload, and all the fallback formats will load as they normally do.
    And your css font face should look similar to to this

    @font-face {
        font-family: FontOne;
        src: url(../somefolder/font-one.eot);
        src: url(../somefolder/font-one.eot?#iefix) format('embedded-opentype'),
        url(../somefolder/font-one.woff2) format('woff2'), //Will be preloaded
        url(../somefolder/font-one.woff) format('woff'),
        url(../somefolder/font-one.ttf)  format('truetype'),
        url(../somefolder/font-one.svg#svgFontName) format('svg'); 
    }
    @font-face {
        font-family: FontTwo;
        src: url(../somefolder/font-two.eot);
        src: url(../somefolder/font-two.eot?#iefix) format('embedded-opentype'),
        url(../somefolder/font-two.woff2) format('woff2'), //Will be preloaded
        url(../somefolder/font-two.woff) format('woff'),
        url(../somefolder/font-two.ttf)  format('truetype'),
        url(../somefolder/font-two.svg#svgFontName) format('svg');
    }
    

提交回复
热议问题