Javascript 幻灯片可点击按钮

发布于 2024-11-14 21:12:49 字数 803 浏览 3 评论 0原文

我正在寻找有关我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-11-21 21:12:49

除非您只是将其作为学习更多 JavaScript 知识的练习,否则比自制幻灯片更好的选择可能是使用 DOM 库。有一个关于创建 幻灯片的非常好的教程SixRevisions 的 jQuery

还有许多针对各种 DOM 库的预构建幻灯片插件 jQuery 有很多

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

然后,您可以使用 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.

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. You would also need to attach a mouseout event to call setTimeout() again to restart the slide show. That said, this is a situation you should really be using setInterval() and clearInterval() instead of 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; all modern browsers understand JavaScript and don't need that hack to hide JS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文