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

Infinite looped vertical slider with no pause

我写了一个脚本

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

它运行良好,在一分钟内滚动了几乎 2000px 个图像,但最后它停止了。我需要它来继续下一张图片(相同的图片,只是重复)并继续......我已经尝试了几件事,但似乎无法正确处理。如何让它连续,并删除间隔末尾的 stop/pause?

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

// 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();