Display a loading bar before the entire page is loaded

前端 未结 3 1085
名媛妹妹
名媛妹妹 2020-11-29 16:15

I would like to display a loading bar before the entire page is loaded. For now, I\'m just using a small delay:

$(document).ready(function(){
    $(\'#page\')         


        
3条回答
  •  甜味超标
    2020-11-29 16:37

    Use a div #overlay with your loading info / .gif that will cover all your page:

    Loading Loading...

    jQuery:

    $(window).load(function(){
       // PAGE IS FULLY LOADED  
       // FADE OUT YOUR OVERLAYING DIV
       $('#overlay').fadeOut();
    });
    

    Here's an example with a Loading bar:

    jsBin demo

      

    CSS:

    *{margin:0;}
    body{ font: 200 16px/1 sans-serif; }
    img{ width:32.2%; }
    
    #overlay{
      position:fixed;
      z-index:99999;
      top:0;
      left:0;
      bottom:0;
      right:0;
      background:rgba(0,0,0,0.9);
      transition: 1s 0.4s;
    }
    #progress{
      height:1px;
      background:#fff;
      position:absolute;
      width:0;                /* will be increased by JS */
      top:50%;
    }
    #progstat{
      font-size:0.7em;
      letter-spacing: 3px;
      position:absolute;
      top:50%;
      margin-top:-40px;
      width:100%;
      text-align:center;
      color:#fff;
    }
    

    JavaScript:

    ;(function(){
      function id(v){ return document.getElementById(v); }
      function loadbar() {
        var ovrl = id("overlay"),
            prog = id("progress"),
            stat = id("progstat"),
            img = document.images,
            c = 0,
            tot = img.length;
        if(tot == 0) return doneLoading();
    
        function imgLoaded(){
          c += 1;
          var perc = ((100/tot*c) << 0) +"%";
          prog.style.width = perc;
          stat.innerHTML = "Loading "+ perc;
          if(c===tot) return doneLoading();
        }
        function doneLoading(){
          ovrl.style.opacity = 0;
          setTimeout(function(){ 
            ovrl.style.display = "none";
          }, 1200);
        }
        for(var i=0; i

提交回复
热议问题