简体   繁体   English

幻灯片javascript无法正常工作

[英]slideshow javascript not working

I am using below java script code to create slide show with images img1.jpg, img1.jpg and img3.jpg. 我正在使用下面的Java脚本代码来创建带有图像img1.jpg,img1.jpg和img3.jpg的幻灯片。 But it is displaying just img1.jpg in the output. 但是它在输出中仅显示img1.jpg。 Please tell the problem. 请告诉问题。

<html>
<style>
#slideshow{width:310;height:210;border-style:solid;}
#slideshow>img{position:absolute;left:15;top:15}
</style>
<body>
<div id="slideshow">
<img id="imge" src="img1.jpg" height="200" width="300">
</div>
<script>
var count=2;
function mf()
{
document.getElementById("imge").src="img"+count+".jpg";
document.getElementById("imge").height="200";
document.getElementById("imge").width="300";
if(count<3)
count++;
else
count=1;
setTimeout("mf()",3000);
}
</script>
</body>
</html>

You need to start off your script afterwards, with mf(); 之后,您需要使用mf()开始脚本。

<script>
   var count=2;
   // if your script is at the bottom of the page, you can move 
   // these three lines out here
   var elem = document.getElementById("imge");  
   elem.height="200";
   elem.width="300";

   function mf() {

      elem.src="img"+ (count + 1) +".jpg"; // add 1 to grab images 1 through 3

      count = ((count + 1) % 3); // will loop from 0 to 2 and back again
      setTimeout(mf,3000);
   }
   mf(); // kick off the first iteration of the slideshow
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM