$("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 ( X > 0 ) { alert("left 2 right"); } else if ( X < 0 ) { alert("right 2 left"); } else if ( Y > 0) { alert("top 2 bottom"); } else if ( Y < 0 ) { alert("bottom 2 top"); } else{ alert("just touch"); }});
The judgment is simple, the final coordinates of the touchmove minus the starting coordinates of the Touchstart, and if the result of X is positive, the finger is drawn from left to right, and if the result of X is a negative number, the finger is drawn from right to left, and if the result of Y is positive, the finger is drawn from the top down. The result of Y, if negative, indicates that the finger is drawn from the bottom upwards.
This is no longer logically a problem. But in the actual operation, the finger up and down sliding in fact is very difficult to round bow, as long as a little bit oblique, will be the x-axis judgment to take over.
Then add a special judgment trick:
$("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("left 2 right"); } else if ( Math.abs(X) > Math.abs(Y) && X < 0 ) { alert("right 2 left"); } else if ( Math.abs(Y) > Math.abs(X) && Y > 0) { alert("top 2 bottom"); } else if ( Math.abs(Y) > Math.abs(X) && Y < 0 ) { alert("bottom 2 top"); } else{ alert("just touch"); }});The increase in the judgment is also very simple, nothing more than to determine which difference is larger. So there's basically no mistake.
Research summary of mobile browser touch events