Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get horizontal scroll event in js

What I want to do, is to catch the event when the user is scrolling div horizontally. For vertical scroll I am using event 'mousewheel' and it's working properly. ( horizontal scroll is performed by a two finger drag on the touchpad - I am testing on Mac OS).

like image 393
gniewleone Avatar asked Nov 05 '12 13:11

gniewleone


People also ask

How do I get horizontal scroll position?

You can use $('html, body'). scrollLeft() to get the horizontal scrolling position if you use jQuery.

How do I use scrollLeft?

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 I get scroll position?

To get or set the scroll position of an element, you follow these steps: First, select the element using the selecting methods such as querySelector() . Second, access the scroll position of the element via the scrollLeft and scrollTop properties.


1 Answers

You can handle horizontal scrolling by :

$("#someContainer").on("scroll", function (e) {
            horizontal = e.currentTarget.scrollLeft;
            vertical = e.currentTarget.scrollTop;
            });

In this case this bind all kind of scroll events on this element so you can also handle

Vertical by e.currentTarget.scrollTop

and

Horizontal by e.currentTarget.scrollLeft

like image 148
Mateusz Rogulski Avatar answered Sep 28 '22 09:09

Mateusz Rogulski