How can I display a loading gif until an entire html page has been loaded

后端 未结 3 1016
我寻月下人不归
我寻月下人不归 2020-12-08 16:15

I have a webpage that\'s pretty intensive via HTML and CSS, which leads to some elements loading faster then others when a user visits the page. The background may take awhi

3条回答
  •  星月不相逢
    2020-12-08 17:10

    Use the following HTML (at the top of the body is best):

    And this CSS:

    #loading {
        background: url('spinner.gif') no-repeat center center;
        position: absolute;
        top: 0;
        left: 0;
        height: 100%;
        width: 100%;
        z-index: 9999999;
    }
    

    And the following JavaScript (uses jQuery):

    function hideLoader() {
        $('#loading').hide();
    }
    
    $(window).ready(hideLoader);
    
    // Strongly recommended: Hide loader after 20 seconds, even if the page hasn't finished loading
    setTimeout(hideLoader, 20 * 1000);
    

    You could put the styles inline on the div instead of in a stylesheet for less chance of a flash of content before the loader. Also, you could use https://www.askapache.com/online-tools/base64-image-converter/ or a similar tool to convert your GIF to a base 64 URI, and use that instead of spinner.gif.

提交回复
热议问题