简体   繁体   English

Javascript 幻灯片可点击按钮

[英]Javascript slideshow clickable buttons

I'm looking for a little help with my javascript slideshow, for which the code was used from a tutorial on the internet.我正在为我的 javascript 幻灯片寻求一点帮助,该幻灯片的代码来自互联网上的教程。 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/ 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.在此先感谢,尼克。

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.除非您只是作为练习来了解有关 JavaScript 的更多信息,否则比自制幻灯片更好的选择可能是使用 DOM 库。 There's a pretty good tutorial on creating a slideshow with jQuery at SixRevisions. SixRevisions 上有一个关于使用 jQuery 创建幻灯片的非常好的教程。

There are also a number of pre-built slideshow plugins for the various DOM libraries jQuery has a bunch .还有许多用于各种 DOM 库的预构建幻灯片插件 jQuery有一堆

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.如果您要继续使用当前代码,要在鼠标悬停时创建暂停,您首先需要创建一个变量并将setTimeout()的结果存储在其中。

var timer;
timer = setTimeout(slideIt, 5000);  // note I just passed the function name instead
                                    // of quoting an invocation.  Using "slide()"
                                    // creates an unnecessary eval() call.

You could then use the timer variable to call clearTimeout() in a mouseover event you attach to the the image.然后,您可以使用timer变量在附加到图像的鼠标悬停事件中调用clearTimeout() You would also need to attach a mouseout event to call setTimeout() again to restart the slide show.您还需要附加 mouseout 事件以再次调用setTimeout()以重新启动幻灯片放映。 That said, this is a situation you should really be using setInterval() and clearInterval() instead of setTimeout() .也就是说,这种情况你应该真正使用setInterval()clearInterval()而不是setTimeout()

You don't need the <!-- and //--> unless your expect you code to be running in ancient browsers like Netscape 1.0 and IE 2;你不需要<!--//-->除非你希望你的代码在像 Netscape 1.0 和 IE 2 这样的古老浏览器中运行; all modern browsers understand JavaScript and don't need that hack to hide JS.所有现代浏览器都理解 JavaScript 并且不需要那个 hack 来隐藏 JS。

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

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