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

后端 未结 3 1012
我寻月下人不归
我寻月下人不归 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 16:56

    I have implemented in Laravel and it worked as expected,

    <style>
    .loader {
                position: fixed;
                left: 0px;
                top: 0px;
                width: 100%;
                height: 100%;
                z-index: 9999;
                background-color: #ffffffcf;
            }
            .loader img{
                position: relative;
                left: 40%;
                top: 40%;
            }
    </style>
    
    <div class="loader" ><img src="{{asset('public/img/loader.gif')}}"></div>
    
    <script>
        window.onload = function() 
        {
            //display loader on page load 
            $('.loader').fadeOut();
        }
    
    </script>
    
    0 讨论(0)
  • 2020-12-08 17:05
        <div id="overlay"></div>
    <style>
        #overlay {
            position: fixed;
            background: rgba(0,0,0,0.8);
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
        }
        .hide {
            display: none;
        }
    </style>
    <script>
        $(window).load(function() {
         $('#overlay').addClass('hide');
        });
    </script>
    
    0 讨论(0)
  • 2020-12-08 17:10

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

    <div id="loading"></div>
    

    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.

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