Jquery Scroll the event to $ (window), find the position difference

I use:

$(window).scroll(function(e){
.....
});

How can I find out the number of pixels (and direction, if the absolute number) that were scrolled?

Thanx

+5
source share
4 answers

Use scrollTop to determine what you are looking for.

+7
source

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;
    });
});
+12
source

Firefox 19 ( : 2013).

var lastScroll = document.body.scrollTop;

, -, jQuery:

var lastScroll = $(document).scrollTop();

:

$(function(){
    var lastScroll = $(document).scrollTop();
    $(window).scroll(function(e) {
        var newScroll = $(document).scrollTop();
        console.log(newScroll - lastScroll);
        lastScroll = newScroll;
    });
});

!

+2

:

$('selector').bind('scroll', function(e){
    e.currentTarget.scrollTop; // .scrollLeft ...
});
+2

All Articles