Home  >  Article  >  Web Front-end  >  How to load images sequentially in javascript_javascript skills

How to load images sequentially in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:49:501369browse

The example in this article describes the method of sequentially loading images in javascript. Share it with everyone for your reference. The details are as follows:

Javascript monitors whether an image has been loaded. If the loading is completed, it will load the next image. It is not loaded from the server all at once to reduce server pressure.
Usable places: For example, when making an application similar to Google Maps, small pictures can be loaded one by one

function Load_pic(arr){
 this.loop_f=function(i,o_file,len,f,obj){
   if(i<len-1){
     i=i+1;
     f(i,o_file,len,obj);
     }
  };
  this.creat_pic=function(i,o_file,len,obj){
    var f=arguments.callee,
    doc=document,
    image = doc.createElement("img");  
    image.src =o_file[i];
    i<len&#63;doc.getElementsByTagName("body")[0].appendChild(image):'';
    if(navigator.userAgent.indexOf("MSIE")>0){ 
        if($.browser.version==6.0 || $.browser.version==9.0){
        //IE9和IE6一样 微软真是怪异
          image.onreadystatechange = function () { 
            if (image.readyState == "complete"){ 
              obj.loop_f(i,o_file,len,f,obj);
            } 
          }; 
        }else{ 
          ie7imagetime = window.setInterval(function(){ 
            var rs = image.readyState; 
            if(rs=="complete"){ 
              window.clearInterval(ie7imagetime); 
              obj.loop_f(i,o_file,len,f,obj);
            }else{ 
              return; 
            } 
          },200); 
        } 
      }else{ 
        image.onload = function () { 
          if (image.complete == true){ 
          obj.loop_f(i,o_file,len,f,obj);
          } 
        }; 
    }
  }; 
   if(arr.constructor===Array){
   var len=arr.length,
      i=0;
   i<len&#63;this.creat_pic(i,arr,len,this):'';
  };
 }
//调用方法
new Load_pic([
'http://gomap.dashilan.cn/jquery-mobile/map/cq/1/img_1/0_0.gif',
'http://gomap.dashilan.cn/jquery-mobile/map/cq/1/img_1/0_1.gif',
'http://gomap.dashilan.cn/jquery-mobile/map/cq/1/img_1/0_2.gif',
'http://gomap.dashilan.cn/jquery-mobile/map/cq/1/img_1/0_3.gif',
'http://gomap.dashilan.cn/jquery-mobile/map/cq/1/img_1/1_0.gif',
'http://gomap.dashilan.cn/jquery-mobile/map/cq/1/img_1/1_1.gif',
'http://gomap.dashilan.cn/jquery-mobile/map/cq/1/img_1/1_2.gif',
'http://gomap.dashilan.cn/jquery-mobile/map/cq/1/img_1/1_3.gif'
]);
//注意要调用jquery 用于判断浏览器

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn