views:

70

answers:

1

Hello,

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?

Thanks a lot :)

+2  A: 

There isn't a specific event, but you could test the .scrollLeft() position to see if it has moved from a previously stored position.

Something like this:

var prevLeft = 0;
$(document).scroll( function(evt) {
    var currentLeft = $(this).scrollLeft();
    if(prevLeft != currentLeft) {
        prevLeft = currentLeft;
        console.log("I scrolled vertically.");
    }
});
patrick dw
I don't know why I didn't even think of that. Thanks a lot that's exactly what I was looking for. (I just used scrollTop() instead because it fitted my situation)
Michael Lumbroso
@Michael - You're welcome. :o)
patrick dw