【问题标题】:Execute scrollTop animation on $(window).scroll with jquery使用 jquery 在 $(window).scroll 上执行 scrollTop 动画
【发布时间】:2015-07-21 07:30:46
【问题描述】:

每次在页面中滚动时,我都会尝试移动一次幻灯片。但是滚动事件不会停止并重复该事件。如何制作动画以移动到下一个幻灯片颜色,其中滚动顶部在 $(window).scroll 内每次只有一次?见我的Fiddle

这是一段不起作用的代码:(

$('html, body').animate({
    scrollTop: ($(next).offset().top)
},500);

我的目标是http://www.sincedilla.com/

【问题讨论】:

  • 它适用于我的 chrome。你有错误吗?
  • 每次滚动时我都需要在每张幻灯片中停下来,然后如果我再次滚动,它必须移动到下一张幻灯片。
  • 你到底想用动画做什么。?你能告诉我吗?你是想平滑滚动还是什么?
  • @lesrpo if($(next)**.stop()**.prev().length > 0 ){ 在您的其他条件中,这可能对您有用
  • @HimeshAadeshara...是的,我想要一个流畅的滚动...像这样sincedilla.com :)

标签: javascript jquery


【解决方案1】:

这可能是您需要的。 在动画完成之前要阻止滚动事件, 动画文档http://api.jquery.com/animate/ 阅读回调部分

$(this).bind('mousewheel', function (e) {
    if (!animating) {
        animating = true;
        if (e.originalEvent.wheelDelta < 0) {
            next = $(first).next();
            first = $(next);
            // scroll down
            $("html, body").animate({
                scrollTop: ($(next).offset().top)
            }, 900, function(){
                animating = false;
            });
        } else {
            first = $(next).prev();
            next = $(first);
            // scroll up
            $("html, body").animate({
                scrollTop: ($(first).offset().top)
            }, 900,function(){
                animating = false;
            });
        }
    }
    return false;
});

工作小提琴http://jsfiddle.net/fdbh0no8/

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-04
  • 1970-01-01
  • 2011-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-28
相关资源
最近更新 更多