Example of how to calculate the sliding distance of a touch event on a JS mobile phone, jstouch

Source: Internet
Author: User

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 ;})}

Related Article

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.