0

3 つの div タグでランダムに表示される画像を表示するために Java スクリプトを使用する HTML ファイルを作成しました。クリックすると、画像が左右に移動します。ページで単一のスライドショーを使用するまでは問題なく動作しますが、同じページで複数のスライドショーを試すと問題が発生します。

1 つのスライド ショーのコードは次のとおりです。

**<script>
currentIndx = 2;

MyImages=new Array();

MyImages[0]='1images2.jpeg';

MyImages[1]='1images3.jpeg';

MyImages[2]='1images4.jpeg';

MyImages[3]='artwork.jpg';

MyImages[4]='1images5.jpeg';

imagesPreloaded = new Array(4)

for (var i = 0; i < MyImages.length ; i++)

{
imagesPreloaded[i] = new Image(120,120)

imagesPreloaded[i].src=MyImages[i]
}

/* we create the functions to go forward and go back          */

function Nexter()

{

if (currentIndx<MyImages.length-1){
alert(currentIndx);
document.leftimg.src=document.middleimg.src
document.middleimg.src=document.rightimg.src
document.rightimg.src=imagesPreloaded[++currentIndx].src

}
else {
alert("In nexter else")
currentIndx=0
document.leftimg.src = document.middleimg.src

document.middleimg.src = document.rightimg.src

document.rightimg.src = imagesPreloaded[currentIndx].src

}

writeImageNumber();

}
function Backer()
{
if (currentIndx>0)
{

 --currentIndx; 

alert("In Backer If");

document.rightimg.src=document.middleimg.src

document.middleimg.src=document.leftimg.src

document.leftimg.src=imagesPreloaded[currentIndx].src

}

else 
{


  currentIndx = MyImages.length-1

  alert(MyImages.length +"else");

  document.rightimg.src = document.middleimg.src

  document.middleimg.src = document.leftimg.src

  document.leftimg.src = imagesPreloaded[currentIndx].src

}

writeImageNumber();

}

/*###### function to reload the images and text when refresh is pressed ##### */

function setCurrentIndex()
{
currentIndx=0;

document.theImage.src=MyImages[0];

document.form1.text1.value=Messages[0];

writeImageNumber();

}

</script>

<body onload="setCurrentIndex();automaticly()">

<!-- start of form for image slide show ####################### -->

<div id ="left" style="float:left"> 

<a href="#" onclick="Backer()" ><img src="1images2.jpeg" width="265" height="262" 
border="0" name="leftimg"></a>
</div>

<div id ="middle"  style="float:left">

<a href="#" onclick=""><img src="1images3.jpeg" width="265" height="262" 
 border="0"    name="middleimg"></a>
</div>

<div id ="right" class="compimg" style="float:left"> 
<a href="#" onclick="Nexter()" ><img src="1images4.jpeg"
width="265" height="262"  alt="" border="0"name="rightimg"></a>
</div> 

<!-- end of form for image slide show ####################### -->**



Any suggestions...
4

1 に答える 1

0

スクリプトを JavaScript クラス (オブジェクト) でラップしていないため、使用しているすべての変数にはグローバル スコープ (ページ レベル) があります。複数回使用しようとすると、変数が上書きされると思います。

単一ページで複数のスライドショーを作成しようとしている方法の HTML マークアップを投稿すると、より良い答えを得ることができます...

于 2012-07-12T07:27:06.900 に答える