js簡單處理圖片預加載

<script type="text/javascript">
    var g = 0;
    var loadEnterTimer = null;
    clearInterval(loadEnterTimer);
    loadEnterTimer = setInterval(function () {
      if (g >= 100) {
        clearInterval(loadEnterTimer);
        return;
      }
      g += 1;
      $('.progress-num').text();
      $('.progress-num').text(g + '%');
    }, 40);
    
    window.onload=function(){
      var sources = [
        'images/page3-bg.jpg',
        'images/page3-bar.png',
        'images/page4-btn.png',
        'images/ercode.jpg'];
      var src = '';
      if(window.location.hash == ''){
        for(var i = 0; i < 60; i++){
          if(i<10){
            src = 'images/tinified/bg_0000'+ i +'.jpg';
          }else{
            src = 'images/tinified/bg_000'+ i +'.jpg';
          }
          sources.push(src);
        }
      }
      for(var i = 1; i < 19; i++){
        src = 'images/word2/'+ i +'.png';
        sources.push(src);
      }
      console.log(sources.length)
      loadImages(sources);
    };
    function loadImages(sources, callback){
      var count = 0,  
        images ={},  
        i = 0;   
        for(i; i < sources.length; i++){  
          images[i] = new Image();
          images[i].onload = function(){  
              if(++count >= sources.length){  
                  // callback && callback(images);  
              }  
          }  
          images[i].src = sources[i]; 
        } 
    }
</script>
相關文章
相關標籤/搜索