chrome PC端的模拟已经成功(使用touchstart and touchend),但在移动端却失效了
chrome模拟效果
部分代码:
<div
class="NoteCol"
@touchstart.stop.prevent="gtouchstart"
@touchmove="gtouchmove"
@touchend="gtouchend($event,i)"
>
gtouchstart() {
this.timeOutEvent = setTimeout(() => {
this.timeOutEvent = 0;
// alert("长按事件触发发");
this.isCancel = !this.isCancel;
},500);//这里设置定时器,定义长按500毫秒触发长按事件,时间可以自己改,个人感觉500毫秒非常合适
return false;
},
gtouchend(e,i){
//手释放,如果在500毫秒内就释放,则取消长按事件,此时可以执行onclick应该执行的事件
// console.log(e);
let timeOutEvent = this.timeOutEvent;
clearTimeout(timeOutEvent);//清除定时器
if(timeOutEvent!=0){
//这里写要执行的内容(尤如onclick事件)
this.DialogShow(i);
// alert("你这是点击,不是长按");
}
return false;
},
gtouchmove(){
//如果手指有移动,则取消所有事件,此时说明用户只是要移动而不是长按
let timeOutEvent = this.timeOutEvent;
clearTimeout(timeOutEvent);//清除定时器
timeOutEvent = 0;
}
有一种可能,就是移动端的 touchmove 过于灵敏,或者手指不够稳定,长按过程中不免有移动,导致长按中断。
你可以把 touchmove 的监听移除,如果在手机端正常了的话,说明这种猜测是正确的。
如果确实如此的话,可以这样解决: