Mobile-side JS Touch Event Basics

Source: Internet
Author: User
<span id="Label3"></p><p><p>first, Touch events on the phone</p></p>  Basic events:  touchstart  //trigger touchmove    //finger on screen when finger is just touching the screen touchend    // Trigger when your finger moves away from the screen   The following is Less: touchcancel  //touch process is canceled when the system triggers each event has the following list, such as Touchend targettouches of course 0:  Touches        //list of all fingers on the screen targettouches  //list of all fingers on the element changedtouches  // List of all fingers that involve the current event each event has a list, and each list has the following properties:  copy code where coordinates are used Pagex,pagey:pagex    //relative to the X-coordinate of the page pagey    // Relative to the y-coordinate of the page Clientx  //relative to the Viewport's x-coordinate clienty  //relative to the Viewport's y-coordinate Screenx  //relative to the x-coordinate of the screen Screeny  //relative to the y-coordinate of the screen &NB Sp;identifier//the unique number of the current touch point target  //the DOM element that the finger touches to copy code other related events:  event.preventdefault ()  /block Touch Browser zoom, scroll bar scrolling var Supporttouch = "createtouch" in document  //to determine if touch events are supported more in-depth content? click:http://www.cesclub.com/bw/jishuzhongxin/webjishu/2011/1216/18069.html   , example   Here are the different types of code to get the specific method of sliding, combined with previous ideas, encapsulated, can learn from learning:  copy code var touchfunc = function (obj,type,func) {   // Slide range within 5x5 do click processing, S is start, e is end     VAR init = {x:5,y:5,sx:0,sy:0,ex:0,ey:0};    var stime = 0, etime = 0;    type = type.tolowercase (); &nb sp;    Obj.addeventlistener ("touchstart", function () {        stime = new Date (). getTime ();         INIT.SX = event.targettouches[0].pagex;        Init.sy = Event.targettouch es[0].pagey;        Init.ex = init.sx;        Init.ey = init.sy;    &nbs P   If (type.indexof ("start")! =-1) func ();   }, false);     Obj.addeventlistener (" Touchmove ", function () {        event.preventdefault ();//browser Zoom when blocking touch, scroll bar scrolling         Init.ex = event.targettouches[0].pagex;        Init.ey = event.targettouches[0].pagey;        If (type.indexof ("move")!=-1) func ();   }, false);     Obj.addeventlistener ("touchend", function () {       var Changex = init.sx-init.ex;        var changey = init.sy-init.ey;      &N Bsp If (math.abs (changex) >math.abs (changey) &&math.abs (changey) >init.y) {           //around events             If (changex > 0) {            &NBSP ;   If (type.indexof ("left")!=-1) func ();           }else{        &NB Sp       If (type.indexof ("right")!=-1) func ();           }       }        ELSE if (math.abs (changey) >math.abs (changex) &&math.abs (changex) > Init.x) {           //up/down events             If (changey > 0) {&nbs P               if (type.indexof ("top")!=-1) func ();          &NB Sp }else{    &nbsp           if (type.indexof ("down")!=-1) func ();           }  & nbsp    }        ELSE if (math.abs (changex) <init.x && math.abs (changey) <init.y) {             etime = new Date (). getTime ();           //click events, here Breakdown by Time difference             If ((etime-stime) >) {                If (type.indexof ("long")!=-1) func (); Long press            }            Else {                if (type.indexof ("click")!=-1) func (); When clicking Process            }       }        if (type.indexof ("end")!=-1) func ();   }, false);};<p><p> Mobile-side JS Touch Event Basics </p> </p></span>

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.