小程序踩坑記——長按與點擊事件沖突


對於同一控件同時設置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