jQuery $(window)中jquery Scroll事件,找出位置差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6399021/
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
jquery Scroll event in $(window), find out the position difference
提问by André Al?ada Padez
I'm using:
我正在使用:
$(window).scroll(function(e){
.....
});
How can i find out the number of pixels (and the direction, if absolute number) that were scrolled?
如何找出滚动的像素数(以及方向,如果是绝对数)?
Thanx
谢谢
回答by Mark Eirich
Here is one way:
这是一种方法:
jQuery(function($) {
var lastScroll = document.body.scrollTop;
$(window).scroll(function(e) {
var newScroll = document.body.scrollTop;
console.log(newScroll - lastScroll);
lastScroll = newScroll;
});
});
回答by Fabio Nolasco
The line below doesn't work on Firefox 19 (which I updated this year: 2013).
下面的行在 Firefox 19(我今年更新:2013)上不起作用。
var lastScroll = document.body.scrollTop;
The solution, which will also improve the cross browser compatibility, is to use a jQuery call:
解决方案,这也将提高跨浏览器的兼容性,是使用 jQuery 调用:
var lastScroll = $(document).scrollTop();
Final result:
最后结果:
$(function(){
var lastScroll = $(document).scrollTop();
$(window).scroll(function(e) {
var newScroll = $(document).scrollTop();
console.log(newScroll - lastScroll);
lastScroll = newScroll;
});
});
Enjoy!
享受!
回答by agarcia
You could try using this:
你可以尝试使用这个:
$('selector').bind('scroll', function(e){
e.currentTarget.scrollTop; // .scrollLeft ...
});