微信小程序单击、双击、长按

这篇博客详细介绍了如何在微信小程序中实现对按钮的单击、双击和长按事件的处理。通过绑定不同的事件处理函数,如`bindtap`、`binddoubletap`和`bindlongtap`,并结合触摸开始和结束时间戳来判断用户操作类型,实现了精确的事件响应。同时,还展示了如何防止长按时触发单击事件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

wxml

<view class="container">
  <view class="btn" bindtap="tap" bindtouchstart="touchStart" bindtouchend="touchEnd">
    单击
  </view>
  <view class="btn" bindtap="doubleTap" bindtouchstart="touchStart" bindtouchend="touchEnd">
    双击
  </view>
  <view class="btn" bindlongtap="longTap" bindtouchstart="touchStart" bindtouchend="touchEnd">
    长按
  </view>
  <view class="btn" bindtap="multipleTap" bindlongtap="longTap" bindtouchstart="touchStart" bindtouchend="touchEnd">
    单击/双击/长按
  </view>
</view>

.js

// pages/touchStart/index.js
Page({
  data: {
    // 触摸开始时间
    touchStartTime: 0,
    // 触摸结束时间
    touchEndTime: 0,
    // 最后一次单击事件点击发生时间
    lastTapTime: 0,
    // 单击事件点击后要触发的函数
    lastTapTimeoutFunc: null,
  },
  onLoad: function (options) {},
  // 按钮触摸开始触发的事件
  touchStart: function (e) {
    this.touchStartTime = e.timeStamp;
  },

  // 按钮触摸结束触发的事件
  touchEnd(e) {
    this.touchEndTime = e.timeStamp;
  },
  /// 单击
  tap() {
    wx.showModal({
      title: '提示',
      content: '单击事件被触发',
      showCancel: false
    });
  },
  /// 双击
  doubleTap(e) {
    const that = this;
    // 控制点击事件在350ms内触发,加这层判断是为了防止长按时会触发点击事件
    if (that.touchEndTime - that.touchStartTime < 350) {
      // 当前点击的时间
      var currentTime = e.timeStamp
      var lastTapTime = that.lastTapTime
      console.log(currentTime);
      console.log(lastTapTime)
      // 更新最后一次点击时间
      that.lastTapTime = currentTime
      // 如果两次点击时间在300毫秒内,则认为是双击事件
      if (currentTime - lastTapTime < 300) {
        console.log("double tap")
        // 成功触发双击事件时,取消单击事件的执行
        clearTimeout(that.lastTapTimeoutFunc);
        wx.showModal({
          title: '提示',
          content: '双击事件被触发',
          showCancel: false
        });
      };
    };
  },
  /// 长按
  longTap(e) {
    console.log("long tap");
    wx.showModal({
      title: '提示',
      content: '长按事件被触发',
      showCancel: false
    });
  },

  // 单击、双击
  multipleTap(e) {
    var that = this
    // 控制点击事件在350ms内触发,加这层判断是为了防止长按时会触发点击事件
    if (that.touchEndTime - that.touchStartTime < 350) {
      // 当前点击的时间
      var currentTime = e.timeStamp;
      var lastTapTime = that.lastTapTime;
      // 更新最后一次点击时间
      that.lastTapTime = currentTime;
      // 如果两次点击时间在300毫秒内,则认为是双击事件
      if (currentTime - lastTapTime < 300) {
        console.log("double tap");
        // 成功触发双击事件时,取消单击事件的执行
        clearTimeout(that.lastTapTimeoutFunc);
        wx.showModal({
          title: '提示',
          content: '双击事件被触发',
          showCancel: false
        });
      } else {
        // 单击事件延时300毫秒执行,这和最初的浏览器的点击300ms延时有点像。
        that.lastTapTimeoutFunc = setTimeout(function () {
          console.log("tap");
          wx.showModal({
            title: '提示',
            content: '单击事件被触发',
            showCancel: false
          });
        }, 300);
      };
    };
  },
})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值