Touchstart,touchmove to determine the direction of the slide screen in the phone

Source: Internet
Author: User

Sliding screen Touchstart: triggered when touching the screen, Touchmove: Active process triggered, touchend: triggered when leaving the screen

First get the coordinates x, y when the hand touches the screen

Get x and Y when touching the screen
$ ('body'). Bind ('touchstart', function (e) { = e.originalevent.changedtouches[0].pagex, = e.originalevent.changedtouches[0 ].pagey;});

It then obtains the sliding coordinates and subtracts the previous coordinates with the following coordinates, judging the direction of its slide by the obtained value. Because the hand slide direction is generally not horizontal or vertical, so can use Math.Abs () to compare, for example: slide in the upper right corner, when the distance is greater than the distance to the right, take it up to the distance, that is, sliding upward.

$('Body'). Bind ('Touchmove', Function (e) {//get x, y when sliding the screenEndX = e.originalevent.changedtouches[0].pagex, EndY= e.originalevent.changedtouches[0].pagey; //Get sliding distanceDistancex = endx-StartX; Distancey= endy-Starty; //judging the sliding direction    if(Math.Abs (Distancex) >math.abs (distancey) && distancex>0) {Console.log ('Swipe Right'); }Else if(Math.Abs (Distancex) >math.abs (distancey) && distancex<0) {Console.log ('Swipe left'); }Else if(Math.Abs (Distancex) <math.abs (distancey) && distancey<0) {Console.log ('swipe up'); }Else if(Math.Abs (Distancex) <math.abs (distancey) && distancey>0) {Console.log ('Swipe down'); }Else{Console.log ('Click Not sliding'); }});

Touchstart,touchmove to determine the direction of the slide screen in the phone

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.