Using jQuery to know when @font-face fonts are loaded?

前端 未结 7 2090
日久生厌
日久生厌 2020-11-28 08:35

I am using @font-face and I hate that Firefox shows the default font, waits to load the @font-face font, then replaces it. So the whole page flashes with the new font.

7条回答
  •  星月不相逢
    2020-11-28 09:04

    Perhaps..

    Create a z-index: -10 div and fill it with a lot of text (with a 'normal' font). At document.ready() or another event:

    var originalnumber = $( div ).width() + $( div ).height() + $( div ).offset().top + $( div ).offset().left;
    
    $( div ).css( 'font-family', 'MyPrettyWebfont' );
    
    var interval = setInterval( function() {
        var number = $( div ).width() + $( div ).height() + $( div ).offset().top + $( div ).offset().left;
    
        if ( number !== originalnumber ) {
            // webfont is loaded and applied!
            clearInterval( interval );
        }
    }, 10 );
    

提交回复
热议问题