javascript - 动画 scrollTop 触发滚动事件

标签 javascript jquery html events

我有一些代码可以监听页面的滚动,它负责两件事,当“页面”发生变化时执行必要的操作(有效),同时将 scrollTop 动画化到最近的“页面”但是在这样做会触发滚动事件,因此它会按顺序爬行所有“页面”,直到到达文档末尾。

如何阻止 animate scrollTop 触发滚动事件?有可能吗?

“页面” 因为这是一个 iPad html 页面,每个 1024x768 View 都是一个“页面”

最佳答案

您可以在这篇文章中找到答案:https://stackoverflow.com/a/1659231/237838

You could make write your own code to set the animation value, and set a flag indicating that the change comes from an animation.

For example: (Untested)

var scrollAnimating = false
jQuery.fx.step.scrollTop = function(E) {
    scrollAnimating = true;
    E.elem.scrollTop = E.now;
    scrollAnimating = false;
};

$('#gototop').click(function() {
    $('body').animate({scrollTop:0},3000);
    $(window).scroll(function () {
        if (!scrollAnimating)
            $('body').stop();
    });
    return false;
})

关于javascript - 动画 scrollTop 触发滚动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15435338/

相关文章:

javascript - datepicker 禁用 future 日期并将今天日期显示为默认日期

jquery - Slickgrid 电子表格插件数字排序

jquery - 获取表单的ID?

html - 如何仅当右侧有元素时才在 HTML 元素中设置边距

javascript - 更少的 CSS 不起作用

Javascript charAt() 打破多字节字符串

javascript - "Not a constructor",但类型检查出来

javascript - 基本 Angular 形式验证不起作用

javascript - 有没有办法验证 Sizzle 选择器?

html - 特定背景图片不会显示在我的 Mac 上的帐户下