- tap事件是用来区分移动端的click事件的
- 在移动端,click事件有延迟,延迟主要是判断当前是滑动还是点击,延迟时间大约有300ms
- 为了提升用户体验,tap事件可以降低延迟的时间
下面是实现tap事件的思路
var bindTapEvent = function(dom,callback){
var startTime = 0;
var isMove = false;
dom.addEventListener('touchstart',function(){
startTime = Date.now();
console.log(startTime);
});
dom.addEventListener('touchmove',function(){
isMove = true;
});
dom.addEventListener('touchend',function(e){
console.log(Date.now()-startTime);
if((Date.now()-startTime)<250&&isMove == false){
callback&&callback.call(this,e)
}else{
console.log('失败');
}
isMove = false;
startTime = 0;
});
};
bindTapEvent(box,function(e){
console.log(e);
console.log(this);
console.log('tap事件');
})