復制代碼 代碼如下:
var Imgvalue;
var Count =13; //圖片數量
var Imgs = new Array(Count);
var ImgLoaded =0;
//預加載圖片
function preLoadImgs()
{
alert('圖片加載中請稍等......');
for(var i=0;i<Imgs.length;i++){
Imgs[i]=new Image();
downloadImage(i);
}
}
//加載單個圖片
function downloadImage(i)
{
var imageIndex = i+1; //圖片以1開始
Imgs[i].src = "images/"+imageIndex+".jpg";
Imgs[i].onLoad=validateImages(i);
}
//驗證是否成功加載完成,如不成功則重新加載
function validateImages(i){
if (!Imgs[i].complete)
{
window.setTimeout('downloadImage('+i+')',200);
}
else if (typeof Imgs[i].naturalWidth != "undefined" && Imgs[i].naturalWidth == 0)
{
window.setTimeout('downloadImage('+i+')',200);
}
else
{
ImgLoaded++
if(ImgLoaded == Count)
{
document.getElementById('BtnStart').disabled=false;
document.getElementById('BtnStop').disabled=false;
alert('圖片加載完畢!');
}
}
}
//開始
function RandStart()
{
Init = setInterval('SetRand()',50);
}
//隨機顯示
function SetRand()
{
imageIndex = Math.floor(Math.random()*Count);
document.getElementById("ImgView").src = Imgs[imageIndex].src;
}
//結束
function RandStop()
{
window.clearInterval(Init);
}