Research summary of mobile browser touch events

Source: Internet
Author: User

$("body").on("touchstart", function(e) {

    e.preventDefault();    startX = e.originalEvent.changedTouches[0].pageX,    startY = e.originalEvent.changedTouches[0].pageY;});$("body").on("touchmove", function(e) {    e.preventDefault();    moveEndX = e.originalEvent.changedTouches[0].pageX,    moveEndY = e.originalEvent.changedTouches[0].pageY,    X = moveEndX - startX,    Y = moveEndY - startY;    if ( X > 0 ) {        alert("left 2 right");    }    else if ( X < 0 ) {        alert("right 2 left");    }    else if ( Y > 0) {        alert("top 2 bottom");    }    else if ( Y < 0 ) {        alert("bottom 2 top");    }    else{        alert("just touch");    }});

The judgment is simple, the final coordinates of the touchmove minus the starting coordinates of the Touchstart, and if the result of X is positive, the finger is drawn from left to right, and if the result of X is a negative number, the finger is drawn from right to left, and if the result of Y is positive, the finger is drawn from the top down. The result of Y, if negative, indicates that the finger is drawn from the bottom upwards.

This is no longer logically a problem. But in the actual operation, the finger up and down sliding in fact is very difficult to round bow, as long as a little bit oblique, will be the x-axis judgment to take over.

Then add a special judgment trick:

$("body").on("touchstart", function(e) {

    e.preventDefault();    startX = e.originalEvent.changedTouches[0].pageX,    startY = e.originalEvent.changedTouches[0].pageY;});$("body").on("touchmove", function(e) {    e.preventDefault();    moveEndX = e.originalEvent.changedTouches[0].pageX,    moveEndY = e.originalEvent.changedTouches[0].pageY,    X = moveEndX - startX,    Y = moveEndY - startY;        if ( Math.abs(X) > Math.abs(Y) && X > 0 ) {        alert("left 2 right");    }    else if ( Math.abs(X) > Math.abs(Y) && X < 0 ) {        alert("right 2 left");    }    else if ( Math.abs(Y) > Math.abs(X) && Y > 0) {        alert("top 2 bottom");    }    else if ( Math.abs(Y) > Math.abs(X) && Y < 0 ) {        alert("bottom 2 top");    }    else{        alert("just touch");    }});The increase in the judgment is also very simple, nothing more than to determine which difference is larger. So there's basically no mistake.

Research summary of mobile browser touch events

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.