微信小程序怎么实现“长按触发”?

 

我的需求是对一个模块进行删除。怎么删除就不说了,我只说怎么实现长按触发一个事件。

思路:

先添加bindtouchstart(手指触摸动作开始事件),记录下触摸开始的时间戳e.timeStamp;再添加bindtouchend(手指触摸动作结束事件),同样记录触摸结束的时间戳e.timeStamp。当两个时间差大于2000毫秒(可以设置其他时间间隔),就触发长按事件,否则触发单击事件。

详情看代码:

 <view bindtap="trunToQRblogDetail" data-blogId="{
   
   {blogItem._id}}"  bindtouchstart="mytouchstart" bindtouchend="mytouchend">
            
</view>
data: {
    touchStartTime:'', //触摸开始时间戳
    touchEndTime:'',   //触摸结束时间戳
  },


 //记录点击开始的时间点
  mytouchstart(e) {
    console.log(e.timeStamp)
    this.setData({
      touchStartTime:e.timeStamp
    })
  },


 //记录点击结束的时间点。如果时间间隔大于2000毫秒,就触发新的事件
  mytouchend(e) {

    this.setData({
      touchEndTime:e.timeStamp
    })

    if(e.timeStamp-this.data.touchStartTime> 2000){
      console.log("触发长按事件")

    }
  },


//单击事件
  trunToQRblogDetail(e) {

   if(this.data.touchEndTime-this.data.touchStartTime < 2000){
   
  }

}

猜你喜欢

转载自blog.csdn.net/lezeqe/article/details/116773145
今日推荐