【发布时间】:2012-04-01 02:17:59
【问题描述】:
您好,我正在构建一个 web 应用程序。为了消除 onclick 延迟,我发现了这个脚本 http://cubiq.org/remove-onclick-delay-on-webkit-for-iphone 代码基本上是-
function NoClickDelay(el) {
this.element = el;
if( 'ontouchstart' in window ){
console.log("===================touch supported :P")
this.element.addEventListener('touchstart', this.handleEvent, false);
}
}
NoClickDelay.prototype = {
handleEvent: function(e) {
switch(e.type) {
case 'touchstart': this.onTouchStart(e); break;
case 'touchmove': this.onTouchMove(e); break;
case 'touchend': this.onTouchEnd(e); break;
}
},
onTouchStart: function(e) {
//e.preventDefault(); //removed to let the page scroll
this.moved = false;
this.element.addEventListener('touchmove', this, false);
this.element.addEventListener('touchend', this, false);
},
onTouchMove: function(e) {
this.moved = true;
},
onTouchEnd: function(e) {
this.element.removeEventListener('touchmove', this, false);
this.element.removeEventListener('touchend', this, false);
if( !this.moved ) {
// Place your code here or use the click simulation below
var theTarget = document.elementFromPoint(e.changedTouches[0].clientX, e.changedTouches[0].clientY);
if(theTarget.nodeType == 3) theTarget = theTarget.parentNode;
var theEvent = document.createEvent('MouseEvents');
theEvent.initEvent('click', true, true);
theTarget.dispatchEvent(theEvent);
}
}
};
我的问题是这适用于 iphone/ipad 但不适用于 Android。是什么阻止它在 android 中工作,我该怎么做才能在 android 和其他设备中实现类似的行为???请帮忙。
【问题讨论】:
标签: javascript android touch touch-event