在两个图像之间循环

时间:2017-03-11 17:41:20

标签: javascript

//此代码用于创建一个包含两个图像的数组,只是圈出它们,使灯泡看起来闪烁。

var imageArray = new Array();

var numImages=2;

// create new array to hold preload images; call this array imageArray   
// create (global!) variable called numImages to hold total number of images;  
//use for loop to populate imageArray    


for (var i = 0; i < numImages; i++) {
imageArray[i] = new image();
imageArray[i].src="images/brightIdea"+(i+1)+"png"
//set image src property to image path, preloading image in the process

}


var i4_circleThru = 0; // global variable ( be careful) use for the function CicleThru()

function circleThru() {

//if browser does not support the image object, exit.

//  write images, from imageArray to HTML doc

// call the setTimeout method on circleThru

}//end circleThru()

1 个答案:

答案 0 :(得分:0)

因为你的最终结果是一个html图像,为什么不这样做:

首先,在您的HTML中:

<img id="imageID">

然后在你的剧本中:

var imageArray = [];
var numImages = 2;
for (var i = 0; i < numImages; i++) {
  imageArray[i] ="images/brightIdea"+(i+1)+".png"
}
var i4_circleThru = 0;
var image = document.getElementById("imageId");
image.src = imageArray[i4_circleThru];

window.setInterval(function(){
  i4_circleThru = (i4_circleThru+1)%numImages;
  image.src = imageArray[i4_circleThru];
},1000);        

可在此处找到更完整的示例: https://jsfiddle.net/FrancisMacDougall/fseswsro/