Judge the direction of touch and slide on the mobile phone

This article is a tool method sharing, the specific code is as follows:

//手机端判断触摸滑动方向
var direction;
var move = {};
$('.load-medio').on('touchstart',function(e){
	//获取接触屏幕时的X和Y
	move.startX = e.originalEvent.changedTouches[0].pageX;
	move.startY = e.originalEvent.changedTouches[0].pageY;
});
$('.load-medio').on('touchmove',function(e) {
	//获取滑动屏幕时的X,Y
	move.endX = e.originalEvent.changedTouches[0].pageX;
	move.endY = e.originalEvent.changedTouches[0].pageY;
});
$('.load-medio').on('touchend',function(e) {
	//获取滑动屏幕时的X,Y
	var distanceX = move.endX - move.startX;
	var distanceY = move.endY - move.startY;
	if (Math.abs(distanceX) > Math.abs(distanceY) && distanceX > 0) {
		direction='right';
	}
	else if (Math.abs(distanceX) > Math.abs(distanceY) && distanceX < 0) {
		direction='left';
	}
	else if (Math.abs(distanceX) < Math.abs(distanceY) && distanceY > 0) {
		direction='down';
	}
	else if (Math.abs(distanceX) < Math.abs(distanceY) && distanceY < 0) {
		direction='up';
		
		$('.load-scr').click();
	}
	
	//console.log(direction);
});
Published 147 original articles · praised 49 · 160,000 views +

Guess you like

Origin blog.csdn.net/bigbear00007/article/details/105461613