var timeOutEvent=0;//定时器
//开始按
function gtouchstart(){
timeOutEvent = setTimeout("longPress()",500);//这里设置定时器,定义长按500毫秒触发长按事件
return false;
};
//如果在500毫秒内就释放,则取消长按事件,此时可以执行onclick应该执行的事件
function gtouchend(){
clearTimeout(timeOutEvent);//清除定时器
return false;
};
//滑动事件
function gtouchmove(){
clearTimeout(timeOutEvent);//清除定时器
timeOutEvent = 0;
};
function longPress(){
timeOutEvent = 0;
alert('触发了长按事件')
}
<div ontouchstart="gtouchstart()" ontouchmove="gtouchmove()" ontouchend="gtouchend()">长按我</div>
来源:CSDN
作者:AloneAsFoam
链接:https://blog.csdn.net/AloneAsFoam/article/details/80523804