移动端点击事件

Posted zengqingsen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了移动端点击事件相关的知识,希望对你有一定的参考价值。


if (!htmlElement.prototype.addTapEvent) {
HTMLElement.prototype.addTapEvent = function(callback) {
var tapStartTime = 0,
tapEndTime = 0,
tapTime = 500, //tap等待时间,在此事件下松开可触发方法
tapStartClientX = 0,
tapStartClientY = 0,
tapEndClientX = 0,
tapEndClientY = 0,
tapScollHeight = 15, //水平或垂直方向移动超过15px测判定为取消(根据chrome浏览器默认的判断取消点击的移动量)
cancleClick = false;
this.addEventListener(‘touchstart‘, function() {
tapStartTime = event.timeStamp;
var touch = event.changedTouches[0];
tapStartClientX = touch.clientX;
tapStartClientY = touch.clientY;
cancleClick = false;
})
this.addEventListener(‘touchmove‘, function() {
var touch = event.changedTouches[0];
tapEndClientX = touch.clientX;
tapEndClientY = touch.clientY;
if ((Math.abs(tapEndClientX - tapStartClientX) > tapScollHeight) || (Math.abs(tapEndClientY - tapStartClientY) > tapScollHeight)) {
cancleClick = true;
}
})
this.addEventListener(‘touchend‘, function() {
tapEndTime = event.timeStamp;
if (!cancleClick && (tapEndTime - tapStartTime) <= tapTime) {
callback();
}
})
}
}

 

//调用

element.addTapEvent(function(){})

以上是关于移动端点击事件的主要内容,如果未能解决你的问题,请参考以下文章

移动端事件

移动端点击事件

fastclick.js --- 解决移动端点击事件300ms延时

解决手机(移动端)点击事件失效问题

ios移动端浏览器点击事件失效的解决方案

移动端click事件延时