【问题标题】:Infinite looped vertical slider with no pause无限循环垂直滑块,无暂停
【发布时间】:2015-06-11 19:12:24
【问题描述】:

我写了一个脚本

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

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

【问题讨论】:

  • 您将间隔设置为仅每分钟重新开始。
  • 正确,那么聪明的选择是什么?
  • 我正在写一个答案:)
  • 当第一个动画完成而不是间隔时再次触发动画,可能

标签: javascript jquery slideshow infinite-scroll vertical-scrolling


【解决方案1】:

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

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

【讨论】:

  • 我遇到了这个错误...未捕获的 TypeError: $elems.at is not a function
  • 抱歉,请将所有.ats 替换为.eq。我修改了答案。
  • Here's a demo 我使用这个答案淡出,然后在所有元素中无限期地一次一个。
  • 啊哈!我明白你的意思了。那就是JQuery animate 的缓动函数,默认设置为"swing"。改为将其设置为 "linear"codepen.io/Chevex/pen/LVLdzj
  • 非常感谢!线性也用原始代码修复了它......我也有上面的代码,并且想了解更多关于 Angular 的知识......再次感谢
猜你喜欢
  • 1970-01-01
  • 2012-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-06
  • 2019-05-25
  • 1970-01-01
相关资源
最近更新 更多