Wait for fonts to load before rendering web page

后端 未结 11 1320
终归单人心
终归单人心 2020-11-27 15:38

I\'m using @font-face to embed fonts in my website. First the text renders as the system default, and then (once the font file has loaded presumably) the correct font render

相关标签:
11条回答
  • 2020-11-27 16:12

    Edit: The best approach is probably to base64 encode your fonts. This means your font will have to be loaded fully by the time your HTML is parsed and displayed. You can do this with font squirrel's webfont generator https://www.fontsquirrel.com/tools/webfont-generator by clicking "Expert" and then "base64 encode". This is how services like TypeKit work.


    Original answer: Another way to detect if fonts are loaded would be using FontLoader https://github.com/smnh/FontLoader or by copying their strategy.

    They bind to the scroll event in the browser, because when the font loads it will resize the text. It uses two containing divs (that will scroll when the height changes) and a separate fallback for IE.

    An alternative is to check the DOM periodically with setInterval, but using javascript events is far faster and superior.

    Obviously, you might do something like set the opacity of body to 0 and then display it in once the font loads.

    0 讨论(0)
  • 2020-11-27 16:16

    This is down to how the browser behaves.

    First off where is your @font declared? Is it inline to your HTML, declared in a CSS sheet on the page, or (hopefully) declared in an external CSS sheet?

    If it is not in an external sheet, try moving it to one (this is better practice anyway usually).

    If this doesn't help, you need to ask yourself is the fraction of a second difference really significantly detrimental to the user experience? If it is, then consider JavaScript, there are a few things you might be able to do, redirects, pauses etc, but these might actually be worse than the original problem. Worse for users, and worse to maintain.

    This link might help:

    http://paulirish.com/2009/fighting-the-font-face-fout/

    0 讨论(0)
  • 2020-11-27 16:21
    (function() {
            document.getElementsByTagName("html")[0].setAttribute("class","wf-loading")
            document.getElementsByTagName("html")[0].setAttribute("className","wf-loading")
        })();
    

    use this method.. use with Webfont.js

    0 讨论(0)
  • 2020-11-27 16:22

    I had a similar problem while rendering to an HTML canvas, and this was my solution. It's based on the FontFace API, and similar to Holtwicks approach. The key differences are that this is a generic approach and that it will work out-of-the-box for external fonts/stylesheets (e.g. google fonts).

    A couple of notes; fonts.load( ... ) will happily resolve with an empty set of fonts if the font isn't known yet. Presumably, this happens if this code is called before the stylesheet declaring the font was added. I added a fonts.check(...) to overcome that.

    This will let you await javascript execution until a font is available, so it won't work out of the box for 'normal' HTML content. You can combine this with Holtwicks answer above.

    export async function waitForFontLoad(
        font: string,
        timeout = 1000,
        interval = 10
    ) {
        return new Promise((resolve, reject) => {
            // repeatedly poll check
            const poller = setInterval(async () => {
                try {
                    await document.fonts.load(font);
                } catch (err) {
                    reject(err);
                }
                if (document.fonts.check(font)) {
                    clearInterval(poller);
                    resolve(true);
                }
            }, interval);
            setTimeout(() => clearInterval(poller), timeout);
        });
    }
    
    0 讨论(0)
  • 2020-11-27 16:25

    You can use CSS font-display inside your @font-face. The keywords for all the available values are:

    • auto
    • block
    • swap
    • fallback
    • optional

    Giulio Mainardi has written a nice article about all of them, and which you should use where on sitepoint.

    You can read it here: https://www.sitepoint.com/css-font-display-future-font-rendering-web/?utm_source=frontendfocus&utm_medium=email

    0 讨论(0)
提交回复
热议问题