微信小程序-长按事件会触发点击事件问题


对于同一控件同时设置bindtap和bindlongtap,会发现长按时先出现bindlongtap的事件,然后触发点击事件。

通过测试,我们发现,小程序中事件执行的顺序是
点击:touchstart → touchend → tap
长按 touchstart → longtap → touchend → tap

处理方法:

// wxml
<view bindtouchstart="bindTouchStart" bindtouchend="bindTouchEnd" bindlongtap="bingLongTap" bindtap="bindTap">测试</view>
// js
bindTouchStart: function(e) {
    this.startTime = e.timeStamp;
}
bindTouchEnd: function(e) {
    this.endTime = e.timeStamp;
}
bindTap: function(e) {
    if(this.endTime  - this.startTime < 350) {
        console.log("点击")
    }
}
bingLongTap: function(e) {
    console.log("长按");
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM