有没有绑定到双击事件(在一个单一的代码行)的移动Safari浏览器的方法吗? 或者,另一种方法是实现它拦截在一些短期给定的时间发生的两个单抽头事件(例如: http://appcropolis.com/blog/implementing-doubletap-on-iphones-and-ipads/ )?
Answer 1:
答案很简单:你必须通过两次点击实现。
实际的答案:这是一个jQuery的免费实现双击移动Safari浏览器只需要的代码(使DBLCLICK事件)一行:
- https://gist.github.com/2927073
此外,您可能会需要禁用移动Safari的默认缩放与此meta标签:
<meta name="viewport" content="width=device-width,user-scalable=no" />
Answer 2:
如果你想有工作双击无论在浏览器和IOS平台,您应该有下面的代码:
jQuery.event.special.dblclick = {
setup: function(data, namespaces) {
var agent = navigator.userAgent.toLowerCase();
if (agent.indexOf('iphone') >= 0 || agent.indexOf('ipad') >= 0 || agent.indexOf('ipod') >= 0) {
var elem = this,
$elem = jQuery(elem);
$elem.bind('touchend.dblclick', jQuery.event.special.dblclick.handler);
} else {
var elem = this,
$elem = jQuery(elem);
$elem.bind('click.dblclick', jQuery.event.special.dblclick.handler);
}
},
teardown: function(namespaces) {
var agent = navigator.userAgent.toLowerCase();
if (agent.indexOf('iphone') >= 0 || agent.indexOf('ipad') >= 0 || agent.indexOf('ipod') >= 0) {
var elem = this,
$elem = jQuery(elem);
$elem.unbind('touchend.dblclick');
} else {
var elem = this,
$elem = jQuery(elem);
$elem.unbind('click.dblclick', jQuery.event.special.dblclick.handler);
}
},
handler: function(event) {
var elem = event.target,
$elem = jQuery(elem),
lastTouch = $elem.data('lastTouch') || 0,
now = new Date().getTime();
var delta = now - lastTouch;
if (delta > 20 && delta < 500) {
$elem.data('lastTouch', 0);
$elem.trigger('dblclick');
} else {
$elem.data('lastTouch', now);
}
}
};
这里试试:
http://jsfiddle.net/UXRF8/
Answer 3:
覆盖dblclick
事件和使用bind
, live
, on
, delegate
像任何其他事件:
jQuery.event.special.dblclick = {
setup: function(data, namespaces) {
var elem = this,
$elem = jQuery(elem);
$elem.bind('touchend.dblclick', jQuery.event.special.dblclick.handler);
},
teardown: function(namespaces) {
var elem = this,
$elem = jQuery(elem);
$elem.unbind('touchend.dblclick');
},
handler: function(event) {
var elem = event.target,
$elem = jQuery(elem),
lastTouch = $elem.data('lastTouch') || 0,
now = new Date().getTime();
var delta = now - lastTouch;
if(delta > 20 && delta<500){
$elem.data('lastTouch', 0);
$elem.trigger('dblclick');
}else
$elem.data('lastTouch', now);
}
};
文章来源: Double tap on mobile safari