jQuery: Every 5 seconds, automatically click on next image in a list?

后端 未结 5 1260
醉话见心
醉话见心 2020-12-21 02:41

I have a given list of images, presented as thumbnails:

  • &
5条回答
  •  萌比男神i
    2020-12-21 03:05

    You may easily calculate the next image within the switch function. This way you won't mess up your var space ;)

    (function switchToImage(img) {
      $(img).click()
      var images = $('#thumbs img');
      var nextIndex = ($.inArray(images, img) + 1) % images.length;
      setTimeout(function() { 
        switchToImage(images[nextIndex]) 
      }, 5000);
    })($('#thumbs img')[0]);
    

提交回复
热议问题