单独一个事件是实现不了的,我们需要两个事件配合使用
$("body").on("touchstart", function (e) {
e.preventDefault();
startX = e.originalEvent.changedTouches[0].pageX,
startY = e.originalEvent.changedTouches[0].pageY;
});
$("body").on("touchmove", function (e) {
e.preventDefault();
moveEndX = e.originalEvent.changedTouches[0].pageX,
moveEndY = e.originalEvent.changedTouches[0].pageY,
X = moveEndX - startX,
Y = moveEndY - startY;
if (Math.abs(X) > Math.abs(Y) && X > 0) {
alert("向右滑动");
}
else if (Math.abs(X) > Math.abs(Y) && X < 0) {
alert("向左滑动");
}
else if (Math.abs(Y) > Math.abs(X) && Y > 0) {
alert("向下滑动");
}
else if (Math.abs(Y) > Math.abs(X) && Y < 0) {
alert("向上滑动");
}
else {
alert("just touch");
}
});