javascript 在 jQuery 中获取滚轮数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11158893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:13:27  来源:igfitidea点击:

Get scroll wheel amount in jQuery

javascriptjquerymousewheel

提问by tskuzzy

After binding the scroll event to an object, how can I get the amount the user scrolled by?

将滚动事件绑定到对象后,如何获取用户滚动的量?

$(selector).scroll(function(e) {
    // get scroll amount
});

Firefox and Opera have the property detailwhereas IE, Safari, and Opera have wheelData. To make matters worse, Firefox and Opera operate on a scale of -3 to 3, and IE and Safari go from -120 to 120.

Firefox 和 Opera 具有该属性,detail而 IE、Safari 和 Opera 具有wheelData. 更糟糕的是,Firefox 和 Opera 的评分范围为 -3 到 3,而 IE 和 Safari 的评分范围为 -120 到 120。

Is there a single, normalized property that jQuery provides for this?

jQuery 是否为此提供了一个单一的、规范化的属性?

回答by snies

Use jQuery .scrollTop()and save the value between scroll events, then get the delta at next scroll event.

使用 jQuery .scrollTop()并保存滚动事件之间的值,然后在下一个滚动事件中获取增量。

var old_scroll_top = 0;

$(document).scroll(function() {
  var current_scroll_top = $(document).scrollTop();
  var scroll_delta = current_scroll_top - old_scroll_top;

  // do something with current_scroll_top and scroll_delta

  old_scroll_top = current_scroll_top;
});

Example:jsFiddle

示例:jsFiddle


Update:


更新:

Hereis a second Example, which shows how to have a canvas, that updates according to scoll Events.

是第二个示例,它展示了如何拥有一个根据 scoll 事件更新的画布。

回答by ply

Could you use scrollTop for this?

您可以为此使用 scrollTop 吗?

var amount = $(window).scrollTop();