show loading icon until the page is load?

前端 未结 7 1302
不知归路
不知归路 2020-11-29 19:42

I wanted to show a loading icon to users until the page elements are fully loaded. How can I do that with javascript and I want to do it with javascript, not jquery?

相关标签:
7条回答
  • 2020-11-29 20:27

    HTML

    <body>
        <div id="load"></div>
        <div id="contents">
              jlkjjlkjlkjlkjlklk
        </div>
    </body>
    

    JS

    document.onreadystatechange = function () {
      var state = document.readyState
      if (state == 'interactive') {
           document.getElementById('contents').style.visibility="hidden";
      } else if (state == 'complete') {
          setTimeout(function(){
             document.getElementById('interactive');
             document.getElementById('load').style.visibility="hidden";
             document.getElementById('contents').style.visibility="visible";
          },1000);
      }
    }
    

    CSS

    #load{
        width:100%;
        height:100%;
        position:fixed;
        z-index:9999;
        background:url("https://www.creditmutuel.fr/cmne/fr/banques/webservices/nswr/images/loading.gif") no-repeat center center rgba(0,0,0,0.25)
    }
    

    Note:
    you wont see any loading gif if your page is loaded fast, so use this code on a page with high loading time, and i also recommend to put your js on the bottom of the page.

    DEMO

    http://jsfiddle.net/6AcAr/ - with timeout(only for demo)
    http://jsfiddle.net/47PkH/ - no timeout(use this for actual page)

    update

    http://jsfiddle.net/d9ngT/

    0 讨论(0)
  • 2020-11-29 20:30

    firstly, in your main page use a loading icon

    then, delete your </body> and </HTML> from your main page and replace it by

    <?php include('footer.php');?>
    

    in the footer.php file type :

    <?php
        $iconPath="myIcon.ico" // myIcon is the final icon
        echo '<script>changeIcon($iconPath)</script>'; // where changeIcon is a javascript function whiwh change your icon.
        echo '</body>';
        echo '</HTML>';
    ?>
    
    0 讨论(0)
  • 2020-11-29 20:40

    The easiest way to put the loader in the website.

    HTML:

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

    CSS:

    #loading {
    position: fixed;
    width: 100%;
    height: 100vh;
    background: #fff url('images/loader.gif') no-repeat center center;
    z-index: 9999;
    }
    

    JQUERY:

    <script>
    jQuery(document).ready(function() {
        jQuery('#loading').fadeOut(3000);
    });
    </script>
    
    0 讨论(0)
  • 2020-11-29 20:41

    Element making ajax call can call loading(targetElementId) method as below to put loading/icon in target div and it'll get over written by ajax results when ready. This works great for me.

    <div style='display:none;'><div id="loading" class="divLoading"><p>Loading... <img src="loading_image.gif" /></p></div></div>
    <script type="text/javascript">
    function loading(id) {
        jQuery("#" + id).html(jQuery("#loading").html());
        jQuery("#" + id).show();
    }
    
    0 讨论(0)
  • 2020-11-29 20:42

    HTML, CSS, JS are all good as given in above answers. However they won't stop user from clicking the loader and visiting page. And if page time is large, it looks broken and defeats the purpose.

    So in CSS consider adding

    pointer-events: none;
    cursor: default;
    

    Also, instead of using gif files, if you are using fontawesome which everybody uses now a days, consider using in your html

    <i class="fa fa-spinner fa-spin">
    
    0 讨论(0)
  • 2020-11-29 20:46

    add class="loading" in the body tag then use below script with follwing css code

    body {
                -webkit-transition: background-color 1s;
                transition: background-color 1s;
            }
            html, body { min-height: 100%; }
            body.loading {
                background: #333 url('http://code.jquery.com/mobile/1.3.1/images/ajax-loader.gif') no-repeat 50% 50%;
                -webkit-transition: background-color 0;
                transition: background-color 0;
                opacity: 0;
                -webkit-transition: opacity 0;
                transition: opacity 0;
            }
    

    Use this code

    var body = document.getElementsByTagName('body')[0];
    var removeLoading = function() {
        setTimeout(function() {
            body.className = body.className.replace(/loading/, '');
        }, 3000);
    };
    removeLoading();
    

    Demo: https://jsfiddle.net/0qpuaeph/

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