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) { StartX = E.originalevent.changedtouches[0].pagex, starty = 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) {    //x/y EndX = E.originalevent.changedtouches[0].pagex when getting the sliding screen    ,    EndY = e.originalevent.changedtouches[0].pagey;    Get sliding distance    Distancex = endx-startx;    Distancey = Endy-starty;    Determine 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.