如何让 jQuery 每隔几秒循环一次点击?
我创建了一个非常基本的图像滑块,可以在单击 div 时更改图像(代码如下)。我想将其设置为每 5 秒自动进行一次。看看其他帖子,似乎我可以用 window.setInterval(event, 5000);
做一些事情,然后让事件每 5 秒触发一次对下一个 div 的点击。不过,我在设置每个功能时遇到了一些麻烦。我的代码如下,我将不胜感激任何帮助:
HTML:
<div id="slider">
<div id="slider-images">
<img class="1" src="assets/slider-1.jpg" width="613" height="344">
<img class="2" src="assets/slider-2.jpg" width="613" height="344">
<img class="3" src="assets/slider-3.jpg" width="613" height="344">
<img class="4" src="assets/slider-4.jpg" width="613" height="344">
<img class="5" src="assets/slider-5.jpg" width="613" height="344">
<img class="6" src="assets/slider-6.jpg" width="613" height="344">
</div>
<div id="slider-nav">
<div class="description" id="1">
<h1>Heading 1</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<div class="description" id="2">
<h1>Heading 2</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<div class="description" id="3">
<h1>Heading 3</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<div class="description" id="4">
<h1>Heading 4</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<div class="description" id="5">
<h1>Heading 5</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<div class="description" id="6">
<h1>Heading 6</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
</div>
jQuery:
$(function() {
//homepage slider
$('#slider-nav div:first').addClass('current');
$('#slider-images img').hide();
$('#slider-images img:first').addClass('current').show();
$('#slider-nav div').click(function(){
var id = $(this).attr('id');
var sliderImg = "#slider-images img";
$(sliderImg).removeClass('current');
$(sliderImg).fadeOut('slow');
$(sliderImg + "." + id).fadeIn('slow');
$("#slider-nav div").removeClass('current');
$(this).addClass('current');
return false;
});
});
I have created a very basic image slider that changes images when a div is clicked (code below). I would like to set it to auto-progress every 5 seconds. Looking at other posts, it seems like I could do something with window.setInterval(event, 5000);
and then have the event fire a click on the next div every 5 seconds. I am having some trouble setting up this each function though. My code is below, I would appreciate any help:
HTML:
<div id="slider">
<div id="slider-images">
<img class="1" src="assets/slider-1.jpg" width="613" height="344">
<img class="2" src="assets/slider-2.jpg" width="613" height="344">
<img class="3" src="assets/slider-3.jpg" width="613" height="344">
<img class="4" src="assets/slider-4.jpg" width="613" height="344">
<img class="5" src="assets/slider-5.jpg" width="613" height="344">
<img class="6" src="assets/slider-6.jpg" width="613" height="344">
</div>
<div id="slider-nav">
<div class="description" id="1">
<h1>Heading 1</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<div class="description" id="2">
<h1>Heading 2</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<div class="description" id="3">
<h1>Heading 3</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<div class="description" id="4">
<h1>Heading 4</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<div class="description" id="5">
<h1>Heading 5</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<div class="description" id="6">
<h1>Heading 6</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
</div>
jQuery:
$(function() {
//homepage slider
$('#slider-nav div:first').addClass('current');
$('#slider-images img').hide();
$('#slider-images img:first').addClass('current').show();
$('#slider-nav div').click(function(){
var id = $(this).attr('id');
var sliderImg = "#slider-images img";
$(sliderImg).removeClass('current');
$(sliderImg).fadeOut('slow');
$(sliderImg + "." + id).fadeIn('slow');
$("#slider-nav div").removeClass('current');
$(this).addClass('current');
return false;
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
Try this:
我认为您最好使用滚动器库,例如 jQuery Cycle: http://jquery.malsup.com /cycle/
它很容易实现,占用空间小,示例很多,您只需要稍微修改 HTML 即可实现它。它也解决了你的问题。
如果你真的想要你的解决方案,这个怎么样(修改@Jeff's)
I think you'll be better off using a scroller library such as jQuery Cycle: http://jquery.malsup.com/cycle/
It's easy to implement, has a small footprint, lots of examples, and you would just need to modify your HTML a little bit to implement it. It solves your problem too.
If you really want your solution, how about this (modified @Jeff's)
基本上使 click 函数成为一个实际的命名函数,然后每 5 秒调用一次,如下所示:
Basically make the click function an actual named function, then call that every 5 seconds like so: