當我們在一個標簽上同時設置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("長按");
}
