The things that move the front end work---touch

Source: Internet
Author: User

In the Internet, the general user is through the mouse click, mouse hover, mouse away and so on to perform some front-end events, but in the mobile front-end, the user is through the finger to operate. Thus the gesture event of the mobile front end is derived. The Click event in the mouse can also be triggered on the mobile front, but this event will have a delay of about half a second on the mobile side. So it is generally not used.
There are generally four cases of gesture events on the mobile side:
Touchstart: Triggers when the finger is placed on the screen
Touchmove: Triggers when the finger moves on the screen
Touchend: Triggers when the finger picks up from the screen
Touchcancel: Triggered when the system cancels the touch event. The first three are widely used, and the fourth one has fewer applications. The fourth item that has been done so far has not been covered. If someone has used it, you can share it with me.
The first three can be seen as a process of user action. They are generally used in conjunction with each other. Let me give you an example of a 360° rotation, which is explained in detail by code.

<! DOCTYPE html>

  

In the code, an element main_swipe is defined, which is the object to be manipulated by the gesture.

Main_swipe.addeventlistener ("Touchstart", function (event) {//defines the coordinates of the initial slide originalcoord.x = event.targettouches[0].  PageX;  Originalcoord.y = Event.targettouches[0].pagey;  Shiftcoord.x = Event.targettouches[0].pagex;  Shiftcoord.y = Event.targettouches[0].pagey;  Finalcoord.x = originalcoord.x;  Finalcoord.y = Originalcoord.y; }, False); [/run] We add listener events for this object, touchstart means that when the finger is just placed on the screen, we can get the coordinates of the current touch point through the event object. namely Event.targettouches[0].pagex; Event.targettouches[0].pagey; where event.targettouches is an array that contains touch data. The first item of its default array is the current set of coordinates. Through Event.targettouches[0].pagex; Event.targettouches[0].pagey, so that we can get the x and y coordinates of the current touch point. Next we add the Touchmove event in order to continue to get the coordinates of the element's touch movement.  [Code]main_swipe.addeventlistener ("Touchmove", function (event) {//defines the coordinates in the slide event.preventdefault ();  Finalcoord.x = Event.targettouches[0].pagex;    Finalcoord.y = Event.targettouches[0].pagey; When the transverse sliding distance is greater than 5 o'clock, it is judged to be effective sliding and performing the method of sliding left.  Conversely, swipe to the right.     if (Finalcoord.x-shiftcoord.x > 5) {window.clearinterval (zidongright); Window.clearinterval (ZidoNgleft);     Panduanleft=false;     Panduanright=false;     Defaults.swipeleft ();    Shiftcoord.x = finalcoord.x;     }else if (Finalcoord.x-shiftcoord.x <-5) {window.clearinterval (zidongright);     Window.clearinterval (Zidongleft);     Panduanleft=false;     Panduanright=false;     Defaults.swiperight ();    Shiftcoord.x = finalcoord.x; }}, False);

Note that the default behavior of the browser must be disabled Event.preventdefault () when acquiring the coordinates in the touch movement.
The method of obtaining coordinates in touch motion is the same as the way the touch begins. There is no further elaboration.
By the condition, when the x-coordinate of the touchmove is greater than the x-coordinate of Touchstart, it proves that the user is sliding to the right and vice versa.
Use this judgment to perform defaults.swiperight (), or defaults.swipeleft ();. The function of 360° rotation can be achieved.

When the finger leaves the screen, it is touchend. You also get the coordinates to leave so that you can define the coordinates for the next touchstart.

Main_swipe.addeventlistener ("Touchend", function (event) {   var changey = originalcoord.y-finalcoord.y;    At the end of the slide, coordinate judgment. and perform a left or right method  if (Changey < defaults.threshold.y && changey > (defaults.threshold.y*-1)) {      Changex = originalcoord.x-finalcoord.x;      if (Changex > Defaults.threshold.x) {      window.clearinterval (zidongright);      Window.clearinterval (zidongleft);      Panduanleft=false;      Panduanright=false;      Defaults.swiperight ();   }      if (Changex < (defaults.threshold.x*-1)) {      window.clearinterval (zidongright);      Window.clearinterval (zidongleft);      Panduanleft=false;      Panduanright=false;      Defaults.swipeleft ();}}  }, False);

The above effect is a simple application of a typical touch gesture event. Gesture applications are widely used in mobile front ends. Use the actual project operation to realize its purpose.

The things that move the front end work---touch

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.