Touch issues caused by mobile page click Latency

Source: Internet
Author: User

I. Event capture and bubbling

Let's start with the triggering process of the event, which will be used later.

The DOM2 level event specifies that the event consists of three stages:

① Event Capture Phase

② in the target phase

③ Event Bubbling Phase

The approximate process is that the event is passed from the outermost layer to the inside (capturing phase), reaching the target element (the target phase) that triggered the event, and then bubbling up (bubbling phase) one layer at a time. The listener for the corresponding event of the element binding passed by this process event will be triggered. For example, the element P's child element C is bound to the point-click event, click on element C will first trigger the Click event in P, using dispatch trigger is also.

PS: Event listener is added by Target.addeventlistener (type, listener[, Usecapture]), and the Usecapture parameter represents whether the event is triggered during the capture phase.

Second, the Click event 300ms delay

The Click event in the mobile device and the PC-side will vary, with most touch-based browser devices having a 300ms event-triggering wait time when clicked, because there is a double-tap on the zoom action, which involves the touch device's gesture interaction native design, The device needs to be judged by time by clicking or double clicking.

For this 300ms delay, according to the online search data, can be solved by a few simple methods:

    1. Ignore it ... If the business involved in the response time of the click of a small demand, 300ms can actually be ignored.
    2. In the mobile version of Chrome and Firefox, disabling page scaling eliminates this delay. But only compatible with these two kinds of bad.
    3. In Chrome 32+, the width of the set viewport is less than or equal to the width of the physical device. Because Chrome 32+ is only supported, it is also ignored.
    4. The CSS Touch Action property is set in ie10+. Other browsers may also be supported later. But currently only support ie10+, so also ignored.
    5. Use the touch event instead of the Click event. This must resolve the delay, but it also causes strange problems, which are then transferred to the body.
Iii. Tap events in Zepto

If we use the Touchend event directly instead of the Click event, one obvious thing is that if you swipe onto an element to release the hand then it will trigger the Touchend event of that element, which is obviously not consistent with the click action. Another problem is that after triggering the Touchend event, the Click event is also triggered after the area 300ms, and if an element disappears after touchend, then the element underneath the click area is also triggered by the Click event, which is called a point-through, However, this can be prevented by Event.preventdefault (the default behavior).

To better simulate click events and address the 300ms latency of mobile device clicks, Zepto's Touch module customizes a tap event, which is modeled using touch events. The rationale is to bind the elements to Touchstart and Touchend events, and then determine whether the triggering area of the event is consistent, or trigger a custom click event.

It looks perfect, but in fact the tap event using Zepto even if preventdefault is used in the event callback function, there is a problem with the point. The reason for this is that Zepto used timers when triggering events. The default behavior occurs long before a custom event is triggered, so the click event is not blocked from triggering.

For the moment, regardless of zepto, for the Click event, we can use another plugin to increase the response speed of the Click event, which is fastclick!

Iv. Fastclick

The basic idea of Fastclick is to capture the Touchstart and touchend events of its child elements on a parent element, and then still determine whether the click action is based on the location information, and if so, immediately use dispatchevent to manually distribute the Click event of the child element, This shortens the response event and, when Touchend, uses Preventdefault to block the default event, preventing the Click event from being triggered again after 300ms.

Things seem to have been solved, but the truth is not so perfect. Aside from the performance loss caused by the manual distribution of events, it's no use on Android that preventdefault block touchend triggers the Click event, that is, if Touchend is used on Android, the Click event must be triggered. Read a lot of information and found that the problem can not be solved, some people say touchstart when the call Preventdefault can block click, but the pro-tested No.

At this time, you can only use the more wretched method to solve. We add a flag to the event object when Fastclick is judged as a click action and triggers the Click event. It is known from the previous content that the Click event will first be triggered on the outer container, so we listen for the Click event at the root and do not operate when the event object has flag and block its event dispatch process when it does not exist. This means that the click event listeners that the element is bound to on the element are triggered only by the Click event that is triggered manually.

Document.addeventlistener (' click ',function(event) {if(Event.myclick = =true) {          return true; }         if(event.stopimmediatepropagation) {event.stopimmediatepropagation (); } Else{event.propagationstopped=true;         } event.stoppropagation ();         Event.preventdefault (); return true;}, true);

Although Fastclick can be used to resolve the click latency problem, there are many additional operations added, and the performance loss is unavoidable. So if the demand for a click is not very high, then it is recommended to ignore that delay.

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.