1

基本上,我想做的是使用一些 JavaScript 代码对图像进行洗牌。它所做的只是将当前图像的显示样式更改为block,同时将之前的图像更改为none

HTML 代码:

<body>
<img src="http://bit.ly/yOqqbg" id="image1" style="display: block;">
<img src="http://bit.ly/dezBUZ" id="image2" style="display: none;">
<img src="http://bit.ly/IvM5HE" id="image3" style="display: none;">
</body>​

JavaScript 代码:

var id = ["image1", "image2", "image3"]; //Array with the id's in the document you want        
to shuffle through
var i = 0; //Set inital array element
initiateTimer(); //Get the loop going

function initiateTimer() {       
if (i > id.length) {
    i = 0;
    initiateTimer();
}
setTimeout(function() { changeElement(); }, 2000); //2000 = 2 seconds
}

function changeElement() {  
if (id === 0) {
    document.getElementById(id[2]).style.display = 'none';
    document.getElementById(id[i]).style.display = 'block';
} else {
    document.getElementById(id[i - 1]).style.display = 'none';
    document.getElementById(id[i]).style.display = 'block';
}
i += 1;
initiateTimer();
} ​
4

1 回答 1

0

i === 3你遇到问题时

if (i > id.length) {

应该成为

if (i >= id.length) {

更新:http: //jsfiddle.net/HgNuc/

于 2012-04-18T01:13:31.853 回答