Example of how to calculate the sliding distance of a touch event on a JS mobile phone, jstouch
This article describes how to calculate the sliding distance of a JS Mobile Phone touch event. We will share this with you for your reference. The details are as follows:
Calculate the sliding distance of a gesture on the mobile phone screen. The Code is as follows:
Function wetherScroll () {var startX = startY = endX = endY = 0; var body = document. getElementsByTagName ("body"); body. bind ('touchstart', function (event) {var touch = event.tar getTouches [0]; // coordinates of the sliding start point startX = touch. pageX; startY = touch. pageY; // console. log ("startX:" + startX + "," + "startY:" + startY) ;}); body. bind ("touchmove", function (event) {var touch = event.tar getTouches [0]; // when the gesture slides, the gesture coordinates change constantly, and the coordinates at the last point are the most Endpoint coordinate endX = touch. pageX; endY = touch. pageY; // console. log ("endX:" + endX + "," + "endY:" + endY) ;}) body. bind ("touchend", function (event) {var distanceX = endX-startX, distanceY = endY-startY; // console. log ("distanceX:" + distanceX + "," + "distanceY:" + distanceY); // The Screen width of the mobile device var clientHeight; optional document.doc umentElement. clientHeight; // console. log (clientHeight; * 0.2); // you can check whether the image slides, instead of clicking if (startY! = Math. abs (distanceY) {// if (Math. abs (distanceY)> clientHeight * 0.2) {// implements the someAction1 function to the slide, and implements the someAction2 distanceY function to the slide up. <0? SomeAction1 (): someAction2 () ;}} startX = startY = endX = endY = 0 ;})}