Get horizontal scroll event in js

邮差的信 提交于 2019-12-03 16:40:25

问题


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


回答1:


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




回答2:


I don't have the proper setup to test this, but $.scroll() should do the trick. It's probably preferable to binding the mousewheel event as well. People use all means of scrolling ;)




回答3:


e.currentTarget.scrollX instead of e.currentTarget.scrollLeft and e.currentTarget.scrollY instead of e.currentTarget.scrollTop



来源:https://stackoverflow.com/questions/13233149/get-horizontal-scroll-event-in-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!