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

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

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


其实自己写也不难:

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

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


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

【热门文章】
【热门文章】