wxml:
<view
bindtouchstart="touchStart"
bindtouchmove="touchMove"
bindtouchend="touchEnd" >
</view>
index.js:
var touchStartX = 0;
var touchStartY = 0;
var time = 0;
var interval = "";
var touchMoveX = 0;
var touchMoveY = 0;
Page({
touchStart: function (e) {
touchStartX = e.touches[0].pageX;
touchStartY = e.touches[0].pageY;
interval = setInterval(function () {
time++;
}, 100);
},
touchMove: function (e) {
touchMoveX = e.touches[0].pageX;
touchMoveY = e.touches[0].pageY;
},
touchEnd: function (e) {
var moveX = touchMoveX - touchStartX
var moveY = touchMoveY - touchStartY
if (Math.sign(moveX) == -1) {
moveX = moveX * -1
}
if (Math.sign(moveY) == -1) {
moveY = moveY * -1
}
if (moveX <= moveY) {
if (touchMoveY - touchStartY <= -30 && time < 10) {
console.log("向上滑动")
}
if (touchMoveY - touchStartY >= 30 && time < 10) {
console.log('向下滑动 ');
}
}else {
if (touchMoveX - touchStartX <= -30 && time < 10) {
console.log("左滑页面")
}
if (touchMoveX - touchStartX >= 30 && time < 10) {
console.log('向右滑动');
}
}
clearInterval(interval);
time = 0;
},
})