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 に答える