12

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?

T J
  • 42,762
  • 13
  • 83
  • 138
Michael Lumbroso
  • 4,931
  • 5
  • 27
  • 34

3 Answers3

12

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 horizontally.");
    }
});
Ricardo Anjos
  • 1,417
  • 18
  • 22
user113716
  • 318,772
  • 63
  • 451
  • 440
  • 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 Jul 06 '10 at 14:43
6

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()

asulaiman
  • 1,221
  • 12
  • 18
1

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;
    }
});
JSK NS
  • 3,346
  • 2
  • 25
  • 42
Andrew Gunn
  • 95
  • 1
  • 8