The mobile side determines the user's sliding direction

Source: Internet
Author: User

Ideas:

(1) Get the initial coordinates of the user's sliding finger

(2) Get the finger coordinates at the end of the user's swipe

(3) Compare the absolute value in the X, Y direction to determine whether the sliding direction is around or up or down

(4) Final actual judgment of the specific direction

/* Note Touchstart, touthmove coordinates are stored in e.targettouches[0]*//* touchend coordinates are stored in e.changedtouches[0] */var firstx = 0,firstY = 0, EndX = 0,endy = 0;//initializes the coordinate value Window.addeventlistener ("Touchstart", function (e) {firstx = E.targettouches[0].clientx; Firsty = E.targettouches[0].clienty;}) Window.addeventlistener ("Touchend", function (e) {endx = E.changedtouches[0].clientx;endy = E.changedtouches[0]. Clienty;movex = endx-firstx;//judgment left and right Movey = endy-firsty;//judging up and down if (Math.Abs (MoveX) > 60 | | Math.Abs (Movey) > 60) {//is judged to be sliding, not by clicking on if (Math.Abs (MoveX) > Math.Abs (Movey))) {/* Judging the distance between the lateral movement and the distance between the longitudinal movement, judging whether it is around or up/down * var ele = moveX > 0? "Right": "Left"; alert (ele);} Else{var ele = movey > 0? "Down": "Up"; alert (ele);}})

  

The mobile side determines the user's sliding direction

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.