javascript顺序加载图片的方法
本文实例讲述了javascript顺序加载图片的方法。分享给大家供大家参考。具体如下:
javascript监听一个图片是否加载完毕如果加载完成再加载下一张,不是一次性从服务器加载减少服务器压力,
可用到的地方:比如制作类似google地图的应用,可以使小图一张一张的加载
functionLoad_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){ varf=arguments.callee, doc=document, image=doc.createElement("img"); image.src=o_file[i]; i<len?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(){ varrs=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){ varlen=arr.length, i=0; i<len?this.creat_pic(i,arr,len,this):''; }; } //调用方法 newLoad_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用于判断浏览器
希望本文所述对大家的javascript程序设计有所帮助。