自定义图像洗牌器不工作?

时间:2012-04-18 01:07:03

标签: javascript

基本上,我正在尝试使用一些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();
} ​

1 个答案:

答案 0 :(得分:0)

i === 3时你会遇到问题

if (i > id.length) {

应该成为

if (i >= id.length) {

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