小程序模拟双击事件
双击是 PC 端很常见的事件,但小程序中没有双击事件,我们可以通过两次单击事件的时间差的来模拟双击事件,具体实现如下(基于 uni-app 框架):
export default {
data() {
return {
touchStartTime: 0
}
},
methods: {
doubleClick() {
if (this.touchStartTime === 0) {
this.touchStartTime = new Date().getTime()
setTimeout(() => {
this.touchStartTime = 0
}, 300)
} else {
if (new Date().getTime() - this.touchStartTime <= 300) {
wx.showModal({
showCancel: false,
content: '你双击了'
})
}
this.touchStartTime = 0
}
}
}
}
扫描下方的二维码,在手机上试一试:
