js-Touch screen slide to determine the sliding direction (mobile version)

Source: Internet
Author: User

var startx, Starty;
Get angle
function Getangle (ANGX, angy) {
Return math.atan2 (Angy, ANGX) * 180/MATH.PI;
};

Return direction 1 up 2 down 3 Left 4 right 0 not sliding according to the starting point
function Getdirection (startx, Starty, EndX, Endy) {
var angx = Endx-startx;
var angy = Endy-starty;
var result = 0;

If the sliding distance is too short
if (Math.Abs (ANGX) < 2 && Math.Abs (Angy) < 2) {
return result;
}

var angle = Getangle (ANGX, angy);
if (angle >= -135 && angle <=-45) {
result = 1;
} else if (Angle > $ && Angle < 135) {
result = 2;
} else if ((angle >= 135 && Angle <= 180) | | (Angle >= -180 && Angle <-135)) {
result = 3;
} else if (angle >= -45 && Angle <= 45) {
result = 4;
}

return result;
}
Finger touch Screen
Document.addeventlistener ("Touchstart", function (e) {
StartX = E.touches[0].pagex;
Starty = E.touches[0].pagey;
}, False);
Finger off the screen
Document.addeventlistener ("Touchend", function (e) {
var endx, Endy;
EndX = E.changedtouches[0].pagex;
Endy = E.changedtouches[0].pagey;
var direction = Getdirection (StartX, Starty, EndX, Endy);
switch (direction) {
Case 0:
Alert ("Not sliding! ");
Break
Case 1:
Alert ("Up! ")
Break
Case 2:
Alert ("Down! ")
Break
Case 3:
Alert ("Left! ")
Break
Case 4:
Alert ("Right! ")
Break
Default
}
}, False);

js-Touch screen slide to determine the sliding direction (mobile version)

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.