无限循环的垂直滑块,没有暂停

Infinite looped vertical slider with no pause

本文关键字:暂停 垂直 无限循环      更新时间:2023-09-26

我写了一个脚本

setInterval(function(){ 
  $('#vslides').animate({
    top: "-=1960"
  }, 60000, function(){                                 
    $('.vslide:last').after($('.vslide:first'));
    $('#vslides').css('top', '0px');
  }); 
}, 60000);

它工作得很好,在一分钟内滚动几乎2000px的图像,但是,最后它会停止。我需要它继续到下一个图像(相同的图像,只是重复)并继续前进......我已经尝试了几件事,但似乎无法正确。如何使其连续,并删除间隔结束时的停止/暂停?

你需要某种递归函数。这是一个解决方案,它提供单个函数来对一个元素进行动画处理,但也接受一个回调函数。然后我们创建第二个函数,该函数将递归遍历包装集中的所有元素,调用我们的动画函数,并传入一个回调,该回调将增加我们的索引并再次调用我们的递归函数来动画下一个元素。

// slides an element
function slideIt($elem, callback) {
  /* begin animation */
  $elem.animate({
    top: "-=1960"
  }, 60000, function () {
    /* animation complete do whatever you need here */
    if (callback) {
      /* a callback was provided, invoke it */
      callback();
    }
  });
}
var $elems = $('.all-my-elements');
var index = 0;
// slides a wrapped set of elements one at a time
function slideRecurse () {
  var $elem = $elems.eq(index);
  slideIt($elems.eq(index), function () {
    /* increment index and call slideRecurse again */
    index++;
    // continue animating indefinitely
    // we're at the end so start over
    if (index === $elems.length) {
      index = 0;
    }
    setTimeout(slideRecurse, 0); // no delay. Just using it to queue up the next call on its own stack.
  });
}
// begin sliding all of my $elems one by one
slideRecurse();