JS to determine the direction of the element slide (up or down) mobile end

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.