1. 程式人生 > >移動端點擊事件

移動端點擊事件

dclient eve false 垂直 can height prot {} click


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(){})

移動端點擊事件