learn a little bit every day.
1 varstartx, Starty;2 //Get Angle3 functionGetangle (ANGX, angy) {4 returnMath.atan2 (Angy, ANGX) * 180/Math.PI;5 };6 7 //return direction 1 up 2 down 3 Left 4 right 0 not sliding according to the starting point8 functiongetdirection (StartX, Starty, EndX, Endy) {9 varANGX = EndX-StartX;Ten varAngy = Endy-Starty; One varresult = 0; A - //if the sliding distance is too short - if(Math.Abs (ANGX) < 2 && Math.Abs (angy) < 2) { the returnresult; - } - - varAngle =Getangle (ANGX, angy); + if(Angle >= -135 && angle <=-45) { -result = 1; +}Else if(Angle > && Angle < 135) { Aresult = 2; at}Else if((angle >= 135 && Angle <= 180) | | (Angle >= -180 && Angle <-135)) { -result = 3; -}Else if(Angle >= -45 && Angle <= 45) { -result = 4; - } - in returnresult; - } to //Finger touch Screen +Document.addeventlistener ("Touchstart",function(e) { -StartX = E.touches[0].pagex; theStarty = E.touches[0].pagey; *},false); $ //finger off the screenPanax NotoginsengDocument.addeventlistener ("Touchend",function(e) { - varEndX, Endy; theEndX = E.changedtouches[0].pagex; +Endy = E.changedtouches[0].pagey; A varDirection =getdirection (StartX, Starty, EndX, Endy); the Switch(direction) { + Case0: -Alert ("Not sliding! "); $ Break; $ Case1: -Alert ("Up! ") - Break; the Case2: -Alert ("Down! ")Wuyi Break; the Case3: -Alert ("Left! ") Wu Break; - Case4: AboutAlert ("Right! ") $ Break; - default: - } -},false);
Original address: http://www.cnblogs.com/beiz/p/5817192.html
JS to determine the direction of the element slide (up or down) mobile end