jQuery touch事件如何同时兼容鼠标的移动?

我使用了 touchstart move end来制作基于touch的轮播效果。
然后我发现PC端,是没法用鼠标触发这三个事件的,我应该如何做到兼容?

阅读 5.3k
2 个回答

其实自己写也不难:

var hasTouch = 'ontouchstart' in window,
startEvent = hasTouch ? 'touchstart' : 'mousedown',
moveEvent = hasTouch ? 'touchmove' : 'mousemove',
endEvent = hasTouch ? 'touchend' : 'mouseup',
cancelEvent = hasTouch ? 'touchcancel' : 'mouseup',

后面的事件都绑定在变量上就好了

devicejs判断平台如果是移动端就用touch事件,如果是桌面端就用鼠标事件。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题