0

我创建了一个 HTML 文件,它使用 java 脚本来显示随机显示在 3 个 div 标签中的图像。单击图像向左或向右移动。在我在页面中使用单个幻灯片之前它工作正常,但是当我在同一页面上尝试多个幻灯片时会产生问题。

这是一张幻灯片的代码:

**<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 回答