又一个小巧的图片预加载类

2018-10-15 17:28

阅读:500

复制代码 代码如下:
<!DOCTYPEhtmlPUBLIC-//W3C//DTDXHTML1.0Transitional//EN
<HTML>
<HEAD>
<TITLE>NewDocument</TITLE>
<metahttp-equiv=Content-Typecontent=text/html;charset=UTF-8/>
<SCRIPTLANGUAGE=JavaScript>
<!--
varIE=navigator.appName==MicrosoftInternetExplorer;
varOpera=navigator.appName.toLowerCase()==opera;
varFF=!IE&&!Opera;
functionLoadImage(arrSrc,callBack)
{
this.Length=arrSrc.length;
this.LoadedLen=0;//已经被加载的图片个数
varself=this;
if(self.Length<1)
{
callBack(arrSrc);
return;
}

//经测试,OPERA与别的浏览器加载方式不同,所以特别独立开来...
if(Opera)
{
for(vari=0;i<self.Length;i++)
{
vartmpImg=newImage();
tmpImg.src=arrSrc[i];
tmpImg.onload=function()
{
self.LoadedLen++;
if(self.LoadedLen==self.Length&&callBack)callBack(arrSrc);
}
}
return;
}

this.Load=function()
{
self.LoadedLen++;
if(self.LoadedLen<self.Length)self.DownImg();
elseif(callBack)callBack(arrSrc);
}

this.DownImg=function()
{
vartmpImg=newImage();
tmpImg.src=arrSrc[self.LoadedLen];
if(IE)
{
if(tmpImg.readyState==complete)self.Load();
elsetmpImg.onreadystatechange=function()
{
if(this.readyState==complete)self.Load();
}
}
elsetmpImg.onload=self.Load;
}
this.DownImg();
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<divid=counter></div>
<SCRIPTLANGUAGE=JavaScript>
<!--
vararr=[
vart=newLoadImage(arr,function(arr)
{

varstr=;
for(vari=0;i<arr.length;i++)
{
str+=<imgsrc=+arr[i]+/><br>;
}
document.body.innerHTML+=str;
});
//-->
</SCRIPT>
</BODY>
</HTML>


评论


亲,登录后才可以留言!