干掉微信小程序-解決點擊(bindTap)和長按(bindLongTap)沖突


當我們在一個標簽上同時設置bindtap和bindlongtap時, 會發現長按時先出現bindlongtap的事件,然后緊接着也會觸發點擊事件。
顯然,大部分時候,這並不是我們想要的事件執行結果。
我們知道,微信小程序中事件執行的順序是
點擊:touchstart → touchend → tap
長按 touchstart → longtap → touchend → tap

解決方法:
通過點擊事件來判定是點擊還是長按。

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