jQuery 和触摸/移动:向左/向右滑动即可执行功能
如果移动用户用一根手指向左滑动,而向右滑动则执行另一个功能,如何才能让 jQuery/JavaScript 执行 jQuery 函数?
如果用户向左/向右滑动,我喜欢将scrollLeft和scrollRight动画到某个位置。我正在通过左/右箭头对桌面进行同样的操作,效果非常好。现在我需要同样的方法来在触摸设备上滑动。
(如果可能的话,无需 jQuery Mobile 或其他附加库)
How can I achieve to let jQuery/JavaScript perform a jQuery-Function if a mobile-user swipes with one finger to the left an another function if he swipes to the right?
I like to animate scrollLeft and scrollRight to a certain position if the user swipes left/right. I'm doing the same for desktop with keyboard-navigation trough Arrow Left/Right which works perfectly. Now I need the same for swiping on touch devices.
(if possible without jQuery Mobile or another additional library)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,这是一个很长的答案,但值得:
jQuery(滑动与触摸)pageX 和 pageY 不断返回 0
希望有帮助
ok heres a long answer but it worth it:
jQuery (Swipe vs. Touch) pageX and pageY keep returning 0
Hope it helps
使用以下一些事件,您可以确定滑动的方向
touchstart
:当手指放在屏幕上时发生touchend
:当手指移开时发生from the screentouchmove
:当已放置在屏幕上的手指在屏幕上移动时发生touchcancel
:在手指实际从屏幕上移开之前取消触摸时发生记录
touchstart
上的 x,y 位置以及touchend
上的 x,y 位置,并确定滑动的方向Using some of the following events you can work out which direction a swipe has been made
touchstart
: Occurs when a finger is placed on the screentouchend
: Occurs when a finger is removed from the screentouchmove
: Occurs when a finger already placed on the screen is moved across the screentouchcancel
: Occurs when a touch is cancelled before the finger is actually removed from the screenRecord the x,y position on
touchstart
and again ontouchend
and determine which direction a swipe has been made