Moving end of the sliding direction of judgment

Source: Internet
Author: User

The moving end of the sliding direction of judgment, mainly to determine the use of x, Y axis direction of the increment, which axis increased faster, is which direction.

Get the initial point in Touchstart, StartX, Starty;

Get moving points in Touchmove, MoveX, Movey

Calculate the difference between the two deltax = Movex-startx; DeltaY = Movey-starty;

Then accumulate DeltaX and DeltaY:

Distx + = Math.Abs (deltax)
Disty + = Math.Abs (DeltaY)

if (Distx > Disty && deltax > 0) {
Console.log (' Right slide ')
}
if (Distx > Disty && deltax < 0) {
Console.log (' Left slide ')
}
if (Distx < disty && DeltaY < 0) {
Console.log (' Top slip ')
}
if (Distx < disty && DeltaY > 0) {
Console.log (' slipping ')
}

The idea is this, the code is as follows

<! DOCTYPE html>#box {width:100%;      height:500px;     Background:orange; }    </style> Let Startx,starty,movex,movey,deltax,deltay; Let Distx= 0Let disty= 0Let box= document.getElementById (' box ') Box.addeventlistener (' Touchstart ',function(e) { let point= E.touches? E.touches[0]: E startX=Point.pagex Starty=Point.pagey Distx= 0Disty= 0}) Box.addeventlistener (' Touchmove ',function(e) { let point= E.touches? E.touches[0]: E moveX=Point.pagex Movey=Point.pagey DeltaX= MoveX-StartX DeltaY= Movey-Starty Distx+=Math.Abs (deltax) disty+=Math.Abs (DeltaY) console.log (DISTX, Disty)if(Distx > Disty && deltax > 0) {Console.log (' Right slide ')          }           if(Distx > Disty && deltax < 0) {Console.log (' Left Slide ')          }          if(Distx < disty && DeltaY < 0) {Console.log (' Slide up ')          }           if(Distx < disty && DeltaY > 0) {Console.log (Decline)          }      })    </script> </body>

Moving end of the sliding direction of judgment

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.