Javascript 幻灯片可点击按钮
我正在寻找有关我的 javascript 幻灯片的一些帮助,该幻灯片的代码是从互联网上的教程中使用的。我基本上想做到这一点,以便当您将鼠标悬停在它上面时,图像不会发生变化。另外,我想在幻灯片底部有两个按钮,如下图所示。
http://imageshack.us/photo/my-images/841/slideshowexample。 jpg/
<img src="/wp-content/themes/twentyten/images/slide1.jpg" name="slide" />
<script>
<!--
//variable that will increment through the images
var step=1
function slideit(){
//if browser does not support the image object, exit.
if (!document.images)
return
document.images.slide.src=eval("image"+step+".src")
if (step<3)
step++
else
step=1
//call function "slideit()" every 5 seconds
setTimeout("slideit()",5000)
}
slideit()
//--></div></script>
预先感谢, 缺口。
I'm looking for a little help with my javascript slideshow, for which the code was used from a tutorial on the internet. I want to basically make it so when you mouse over it stops the image from changing. Also, i'd like to have two buttons at the bottom of the slideshow such as the image below.
http://imageshack.us/photo/my-images/841/slideshowexample.jpg/
<img src="/wp-content/themes/twentyten/images/slide1.jpg" name="slide" />
<script>
<!--
//variable that will increment through the images
var step=1
function slideit(){
//if browser does not support the image object, exit.
if (!document.images)
return
document.images.slide.src=eval("image"+step+".src")
if (step<3)
step++
else
step=1
//call function "slideit()" every 5 seconds
setTimeout("slideit()",5000)
}
slideit()
//--></div></script>
Thanks in advance,
Nick.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您只是将其作为学习更多 JavaScript 知识的练习,否则比自制幻灯片更好的选择可能是使用 DOM 库。有一个关于创建 幻灯片的非常好的教程SixRevisions 的 jQuery。
还有许多针对各种 DOM 库的预构建幻灯片插件 jQuery 有很多。
如果您要继续使用当前的代码,要在鼠标悬停时创建暂停,您首先需要创建一个变量并将
setTimeout()
的结果存储在其中。然后,您可以使用
timer
变量在附加到图像的鼠标悬停事件中调用clearTimeout()
。您还需要附加一个 mouseout 事件以再次调用setTimeout()
以重新启动幻灯片放映。也就是说,在这种情况下,您确实应该使用setInterval()
和clearInterval()
而不是setTimeout()
。您不需要
除非您希望代码在 Netscape 1.0 和 IE 2 等古老的浏览器中运行;所有现代浏览器都可以理解 JavaScript,并且不需要那种 hack 来隐藏 JS。
Unless you are just doing this as an exercise to learn more about JavaScript, a better option than a homegrown slideshow would probably be to use a DOM library. There's a pretty good tutorial on creating a slideshow with jQuery at SixRevisions.
There are also a number of pre-built slideshow plugins for the various DOM libraries jQuery has a bunch.
If you were to continue using your current code, to create the pause on mouseover you would first need to create a variable and store the result of
setTimeout()
in it.You could then use the
timer
variable to callclearTimeout()
in a mouseover event you attach to the the image. You would also need to attach a mouseout event to callsetTimeout()
again to restart the slide show. That said, this is a situation you should really be usingsetInterval()
andclearInterval()
instead ofsetTimeout()
.You don't need the
<!--
and//-->
unless your expect you code to be running in ancient browsers like Netscape 1.0 and IE 2; all modern browsers understand JavaScript and don't need that hack to hide JS.