最近接到一个需求,是移动端的长按事件,选择使用touch(触摸)事件来实现。
touchstart事件 | 当手指触摸屏幕时候触发,即使已经有一个手指放在屏幕上也会触发。 |
---|---|
touchend事件 | 当手指从屏幕上离开的时候触发。 |
在需要使用长按事件的地方加上:
<div @touchstart="longTimeTouch()" @touchend="emptyTime()">
在方法中加上:
// 长按事件
longTimeTouch:function (e) {
clearTimeout(this.Loop); //再次清空定时器,防止重复注册定时器
this.Loop = setTimeout(function() {
//执行的代码块
alert("执行了一次长按事件!!!")
}.bind(this), 1500);
},
// 长按结束清空定时器
emptyTime:function () {
clearTimeout(this.Loop); //清空定时器,防止重复注册定时器
},