扩展jQuery的.on()以使用移动触摸事件

我试图使用jQuery移动事件而不使用jQuery mobile的其余部分。

https://github.com/jvduf/jquery-mobile-events/blob/master/jquery.mobile.events.js

该片段可以启用它们,并且可以正常工作,但不能使用.on()事件处理程序。 例如:

$('a').on('tap',function(){ console.log('Hi there!'); }); 

但它确实适用于.live(),但现在已经折旧了。

所以我的问题; 有没有办法扩展.on()function,以包括点击事件和其他? 完整列表如下:

  • touchstart
  • touchmove
  • touchend
  • orientationchange
  • 龙头
  • taphold
  • 刷卡
  • swipeleft
  • swiperight
  • scrollstart
  • scrollstop

谢谢 :)

但它确实适用于.live(),但现在已经折旧了。

所以我认为你想使用事件委托来保留替换元素上的那些事件。 这意味着:

 $('a').on('tap',function () { console.log('Hi there!'); }); 

需要改为:

 $(document).on('tap', 'a', function () { console.log('Hi there!'); }); 

为了使它的行为与$("a").live("tap", ...

也许为移动和桌面扩展JQuery事件代码应该更好。

一种方法是使用JQuery vmouse(虚拟鼠标)插件。

来自vmouse插件评论:

//这个插件是一个抽象触摸和鼠标的实验
//事件使开发人员不必担心输入的方法
//他们的文档加载在支持的设备上。
//
//这里的想法是允许开发人员注册监听器
//基本鼠标事件,例如mousedown,mousemove,mouseup和click,
//并且插件将负责注册正确的侦听器
//在幕后调用最快的时间调用监听器
//对于该设备,同时仍保留事件触发的顺序
//传统的鼠标环境,应该注册多个处理程序
//在不同事件的相同元素上。
//
//当前版本将以下虚拟事件公开给jQuery绑定方法:
//“vmouseover vmousedown vmousemove vmouseup vclick vmouseout vmousecancel”

有关更好的说明,请参阅https://coderwall.com/p/bdxjzg

vmouse插件: https : //github.com/jquery/jquery-mobile/blob/master/js/jquery.mobile.vmouse.js

另请参阅此链接,了解(触摸)事件的当前状态: http : //blogs.adobe.com/adobeandjquery/2011/03/07/the-current-state-of-touch-events/