Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect horizontal scrolling in jQuery?

Tags:

jquery

scroll

How do I detect horizontal scrolling with jQuery?

This will get all scrolls:

$(window).scroll(function () {     alert('in'); }); 

I want just the horizontal one.

like image 870
Aharon Muallem Avatar asked Aug 16 '11 10:08

Aharon Muallem


People also ask

How to find scroll position in jQuery?

The scrollTop() method sets or returns the vertical scrollbar position for the selected elements. Tip: When the scrollbar is on the top, the position is 0. When used to return the position: This method returns the vertical position of the scrollbar for the FIRST matched element.

How can get horizontal scroll position in jQuery?

The scrollLeft() method sets or returns the horizontal scrollbar position for the selected elements. Tip: When the scrollbar is on the far left side, the position is 0. When used to return the position: This method returns the horizontal position of the scrollbar for the FIRST matched element.

How do you know which element is causing horizontal scroll?

Using the devtools To find out which elements cause a horizontal scrollbar, you can use the devtools to delete elements one by one until the scrollbar disappears.

How do I horizontally scroll a div using jQuery?

In jQuery, we can make any element tag move horizontally using the built-in function scrollLeft() function for scrolling the scrollbar horizontally according to either the position which is set as the parameter or without setting the position which would specify the position in pixel number of the scrollbar.


2 Answers

This seems to work.

var lastScrollLeft = 0; $(window).scroll(function() {     var documentScrollLeft = $(document).scrollLeft();     if (lastScrollLeft != documentScrollLeft) {         console.log('scroll x');         lastScrollLeft = documentScrollLeft;     } }); 

jsFiddle.

like image 162
alex Avatar answered Sep 20 '22 19:09

alex


An update that shows direction left or right in a horizontal scroll based on code above:

var lastPos = 0; $(window).scroll(function() {     var currPos = $(document).scrollLeft();      if (lastPos < currPos) {         console.log('scroll right');     }      if (lastPos > currPos)      {         console.log('scroll left');     }      lastPos = currPos; }); 

Test it at http://jsfiddle.net/EsPk7/7/

like image 43
dunderwood Avatar answered Sep 20 '22 19:09

dunderwood