H5移动端判断手势滑动方向

移动端判断手势滑动方向

$("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("从左到右滑动");
	} 
	if(Math.abs(X) > Math.abs(Y) && X < 0) {
		alert("从右到左滑动");
	} 
	if(Math.abs(Y) > Math.abs(X) && Y > 0) {
		alert("从上到下滑动");
	}
	if(Math.abs(Y) > Math.abs(X) && Y < 0) {
		alert("从下到上滑动");
	} 
});
posted @ 2022-07-20 18:17  猫老板的豆  阅读(268)  评论(0编辑  收藏  举报