如何让 jQuery 每隔几秒循环一次点击?

发布于 2024-10-18 00:56:12 字数 2549 浏览 5 评论 0原文

我创建了一个非常基本的图像滑块,可以在单击 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 技术交流群。

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

发布评论

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

评论(3

旧情勿念 2024-10-25 00:56:12

试试这个:

setInterval(function()
{
    var $next = $('#slider-nav div.current').next();
    if ($next.length==0) $next = $('#slider-nav div:first')
    $next.trigger('click')
}, 5000);

Try this:

setInterval(function()
{
    var $next = $('#slider-nav div.current').next();
    if ($next.length==0) $next = $('#slider-nav div:first')
    $next.trigger('click')
}, 5000);
悟红尘 2024-10-25 00:56:12

我认为您最好使用滚动器库,例如 jQuery Cycle: http://jquery.malsup.com /cycle/

它很容易实现,占用空间小,示例很多,您只需要稍微修改 HTML 即可实现它。它也解决了你的问题。

如果你真的想要你的解决方案,这个怎么样(修改@Jeff's)

var $next;
setInterval(function()
{
    if ($next == undefined || $next.next().length == 0) { 
        $('#slider-nav div:first'); 
    } else {
        $next = $next.next();
}
$next.trigger('click');
}, 5000);

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)

var $next;
setInterval(function()
{
    if ($next == undefined || $next.next().length == 0) { 
        $('#slider-nav div:first'); 
    } else {
        $next = $next.next();
}
$next.trigger('click');
}, 5000);
已下线请稍等 2024-10-25 00:56:12

基本上使 click 函数成为一个实际的命名函数,然后每 5 秒调用一次,如下所示:

$('#slider-nav div').click(myClickHandler());
//Might have to do "myClickHandler()" - I cannot test this right now

function myClickHandler()
{
    var id        = $('#slider-nav div').attr('id');
    var sliderImg = "#slider-images img";
    $(sliderImg).removeClass('current');
    $(sliderImg).fadeOut('slow');
    $(sliderImg + "." + id).fadeIn('slow');

    $("#slider-nav div").removeClass('current');
    $('#slider-nav div').addClass('current');
    return false;
}

window.setInterval("myClickHandler()", 5000);

Basically make the click function an actual named function, then call that every 5 seconds like so:

$('#slider-nav div').click(myClickHandler());
//Might have to do "myClickHandler()" - I cannot test this right now

function myClickHandler()
{
    var id        = $('#slider-nav div').attr('id');
    var sliderImg = "#slider-images img";
    $(sliderImg).removeClass('current');
    $(sliderImg).fadeOut('slow');
    $(sliderImg + "." + id).fadeIn('slow');

    $("#slider-nav div").removeClass('current');
    $('#slider-nav div').addClass('current');
    return false;
}

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