Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a vertical scroll event in jQuery

Tags:

jquery

scroll

I have a function that I bound with scroll() event, but the fact is I want the function to be triggered only in case of vertical scroll ( I have some horizontal scroll too).

I didn't see such possibility in the documentation of jQuery, might there be a trick to do so?

like image 919
Michael Lumbroso Avatar asked Jul 06 '10 13:07

Michael Lumbroso


2 Answers

You can also use .scrollTop to make a custom vertical scroll event.

var prevTop = 0;
$(document).scroll( function(evt) {
    var currentTop = $(this).scrollTop();
    if(prevTop !== currentTop) {
        prevTop = currentTop;
        console.log("I scrolled vertically.");
    }
});

Jquery .scrollTop()

like image 76
asulaiman Avatar answered Oct 09 '22 08:10

asulaiman


This should work:

var prevLeft = 0;
$(document).scroll( function(evt) {
    var currentLeft = $(this).scrollLeft();
    if(prevLeft === currentLeft) {
        console.log("I scrolled vertically.");
    } 
    else {
        prevLeft = currentLeft;
    }
});
like image 29
Andrew Gunn Avatar answered Oct 09 '22 08:10

Andrew Gunn