1.對(duì)應(yīng)的xml里寫上手勢(shì)開(kāi)始、滑動(dòng)、結(jié)束的監(jiān)聽(tīng):view class="touch" bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd" /view2.js: view plain copyvar touchDot = 0;//觸摸時(shí)的原點(diǎn) ...
1.對(duì)應(yīng)的xml里寫上手勢(shì)開(kāi)始、滑動(dòng)、結(jié)束的監(jiān)聽(tīng):
<view class="touch" bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd" ></view>
2.js:
[javascript] view plain copy
var touchDot = 0;//觸摸時(shí)的原點(diǎn)
var time = 0;// 時(shí)間記錄,用于滑動(dòng)時(shí)且時(shí)間小于1s則執(zhí)行左右滑動(dòng)
var interval = "";// 記錄/清理時(shí)間記錄
Page({
data: {...}
})
Page({
data: {
...
},
// 觸摸開(kāi)始事件
touchStart: function (e) {
touchDot = e.touches[0].pageX; // 獲取觸摸時(shí)的原點(diǎn)
// 使用js計(jì)時(shí)器記錄時(shí)間
interval = setInterval(function () {
time++;
}, 100);
},
// 觸摸移動(dòng)事件
touchMove: function (e) {
var touchMove = e.touches[0].pageX;
console.log("touchMove:" + touchMove + " touchDot:" + touchDot + " diff:" + (touchMove - touchDot));
// 向左滑動(dòng)
if (touchMove - touchDot <= -40 && time < 10) {
wx.switchTab({
url: '../左滑頁(yè)面/左滑頁(yè)面'
});
}
// 向右滑動(dòng)
if (touchMove - touchDot >= 40 && time < 10) {
console.log('向右滑動(dòng)');
wx.switchTab({
url: '../右滑頁(yè)面/右滑頁(yè)面'
});
}
},
// 觸摸結(jié)束事件
touchEnd: function (e) {
clearInterval(interval); // 清除setInterval
time = 0;
},
.
.
.
})