Principle:
- When starting a Touchstart event, get the horizontal startx and ordinate starty of the finger at this moment;
- When the Touchmove event is triggered, the horizontal moveendx and ordinate moveendy of the finger are obtained at this point, and finally, the direction of the finger on the mobile screen is judged by the coordinate difference obtained two times.
The specific code is as follows:
| 12345678910111213141516171819202122232425262728 |
$("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"); } elseif( X < 0 ) { alert("right 2 left"); } elseif( Y > 0) { alert("top 2 bottom"); } elseif( Y < 0 ) { alert("bottom 2 top"); } else{ alert("just touch"); }}); |
Summary:The final coordinates of the touchmove minus the starting coordinates of the Touchstart.
- The result of X, if positive, indicates that the finger is drawn from left to right;
- The result of X, if negative, indicates that the finger is drawn from right to left;
- The result of Y, if positive, indicates that the finger is drawn from the top down;
- The result of Y, if negative, indicates that the finger is drawn from bottom to top.
The pit has begun ...
The ideal is very plump, show very bony feeling! In the actual operation, the finger up and down sliding difficult to do round bow, as long as a little bit oblique, as long as a little bit oblique, will be the x-axis judgment to take over, and our actual operation will deviate from. At this point you need to add a special judgment technique, the code is as follows:
| 12345678910111213141516171819202122232425262728 |
$("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"); } elseif( Math.abs(Y) > Math.abs(X) && Y > 0) { alert("top 2 bottom"); } elseif( Math.abs(Y) > Math.abs(X) && Y < 0 ) { alert("bottom 2 top"); } else{ alert("just touch"); }}); |
The above code, in the test still does not achieve the desired effect, at this time to notice a fact--$ (' body '). Height = 0 (here is a big hole, but sometimes it will not appear, hope the Great God)
Therefore, you should also add code on this basis:
| 12345 |
var < Code class= "JavaScript plain" >windowheight = $ (window). Height (), $body = $ ( Code class= "JavaScript string" > "body" &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //Console.log ($ (window). Height ()); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //Console.log ($ (' body '). Height ()); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; $body. css ( , windowheight); |
In this, the mobile mobile end of the finger has been implemented on the slide, slide, Zoli and right slide operation.
Touch event on mobile--swipe event of finger