JS Learning Summary----Mobile-based event base and common event library

Source: Internet
Author: User

I. The foundation of the event

Pc:click, MouseOver, Mouseout, MouseEnter, MouseLeave, MouseMove, MouseDown, MouseUp, MouseWheel, KeyDown, KeyUp, load, Scroll, Blur, focus, change ...

Mobile: Click, load, scroll, blur, focus, change, input (instead of KeyUp, KeyDown) ... Touch event Model (handles single-finger operation), gesture event model (handles multi-finger operations)

Touch:touchstart, Touchmove, Touchend, Touchcancel

Gesture:gesturestart, Gesturechange, Gestureend

1, click: In the Mobile click belongs to the clicked event, not the Click event, in the mobile side of the project we often distinguish between what to do and double-click to do what, so the mobile browser in the identification of the click, only determined that it is clicked before it will be executed:

There is a 300ms delay in using the click on the mobile side: After the first click, the browser will have to wait until 300ms to see if the second click is triggered, and if the second click is triggered, it does not belong to the click, and the second click does not appear to belong to the click.

The following code is the code for the mobile-side simulation Click Time

functionOn (CURELE,TYPE,FN) {Curele.addeventlistener (TYPE,FN,false); }        varOBox = Document.queryselector ('. Box ')); //300ms Latency with click on the mobile side        //obox.addeventlistener (' click ', Function () {        //this.style.webkitTransform = ' rotate (360deg) '        //},false)        //Use the Touch event model for click Actions (click && Double-tap)On (OBox, ' Touchstart ',function(EV) {//Ev:touchevent Event Properties type, Target, Preventdefault (returnvalue), stoppropagation, changedtouches, touches            //changedtouches and touches are both the collection of Finger information (touchlist), and the necessary conditions for touches to get the value are only available if the finger is still on the screen. So in the Touchend event, if you want to get the instantaneous coordinates of your finger, you can only use changedtouches to get            varPoint = Ev.touches[0];  This[' strx '] =Point.clientx;  This[' stry '] =Point.clienty;  This[' ismove '] =false; }) on (OBox,' Touchmove ',function(EV) {varPoint = Ev.touches[0]; varNEWX =Point.clientx, Newy=Point.clienty; //to determine if a slip occurs, we need to determine if the value of the offset is within 30PX            if(Math.Abs (newx- This[' Strx ']) >30 | | Math.Abs (newy- This[' Stry ']) >30){                 This[' ismove '] =true; }}) on (OBox,' Touchend ',function(EV) {if( This[' ismove '] = = =false){                //no mobile clicks have occurred                 This. style.webkittransitionduration = ' 1s ';  This. Style.webkittransform = ' rotate (360deg) '; varDelaytimer = Window.settimeout (function(){                     This. style.webkittransitionduration = ' 0s ';  This. Style.webkittransform = ' rotate (0deg) '; }.bind ( This), 1000); }Else{                //Slide                 This. Style.background = ' Red '; }        })

You can also use Fastclick.js to resolve the 300ms latency of the mobile Click event (GitHub address Https://github.com/zhouxiaotian/fastclick)

2. Click, click, double click, long Press, swipe, Zoli, right, slide, slide

Click and double tap (300MS)

Tap and long Press (750MS)

Click and slide (the distance between x/Y axis offset is within 30PX, more than 30PX is sliding)

Swipe left and right (distance between x-axis offset > y-Axis offset = left and right slide instead of up and down)

Left and right slide (offset distance >0 = Right slide opposite left slide)

Second, the common event library

Fastclick.js: Resolving latency for Click event 300MS

Touch.js: Baidu Cloud Mobile gesture Library GitHub address https://github.com/Clouda-team/touch.code.baidu.com

Examples are as follows:  

varOBox = Document.queryselector ('. Box ')); //ClickTouch.on (OBox, ' tap ',function(EV) { This. style.webkittransitionduration = ' 1s ';  This. Style.webkittransform = ' rotate (360deg) '; varDelaytimer = Window.settimeout (function(){                 This. style.webkittransitionduration = ' 0s ';  This. Style.webkittransform = ' rotate (0deg) '; Window.cleartimeout (Delaytimer)}.bind ( This), 1000)        })        //Double clickTouch.on (OBox, ' Doubletap ',function(EV) { This. style.webkittransitionduration = ' 1s ';  This. Style.webkittransform = ' rotate ( -360deg) '; varDelaytimer = Window.settimeout (function(){                 This. style.webkittransitionduration = ' 0s ';  This. Style.webkittransform = ' rotate (0deg) '; Window.cleartimeout (Delaytimer)}.bind ( This), 1000)        })        //Long PressTouch.on (OBox, ' hold ',function(EV) { This. Style.backgroundcolor = ' Red '; })

  Hammer.js

Zepto.js: Known as the mobile side of the small jq,jq because it is used on the PC side, so the code contains a lot of IE low-version browser compatible processing, and zepto only applied on the mobile side development, so on the basis of JQ not on the low version of IE support

JQ provides a number of selector types and DOM manipulation methods, but zepto only implements some of the commonly used selectors and methods. For example: The animated methods in JQ are animate, hide, show, FadeIn, FadeOut, Fadetoggle, Slidedown, Slideup, Slidetoggle ... But in Zepto, only animate

Zepto's source code size is much smaller than JQ

Zepto is specifically designed for mobile development, so it is more suitable for mobile than JQ:

Zepto Animate animation methods support the operation of the CSS3 animation

Zepto specializes in the use of mobile-ready event operations: Tap, Singletap, Doubletap, Longtap, swipe, Swipeup, Swipedown, Swipeleft, swiperight

The instance code is as follows:  

$ ('. Box '). Singletap (function(EV) {$ ( This). Animate ({rotate:' 360deg '            },' Linear ',function(){                 This. Style.webkittransform = ' rotate (0) '            })        })        $('. Box '). On (' Touchstart ',function(){            $( This). CSS (' background ', ' red ')}) $.ajax ({URL:‘‘, type:' Get ', DataType:' JSON ', Cache:false, Success:function(){                            }        })

JS Learning Summary----Mobile-based event base and common event library

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.