Fixed Touchend event not triggered after mobile page scrolling

Source: Internet
Author: User

Problem

When optimizing on a mobile page, it is common to use the touch event instead of mouse-related events, using the Touchend event instead of the PC-side click and MouseUp events.

However, the Touchend event has a problem when the page scrolls. After scrolling is complete, if the current contact position refers to an element that is bound to the Touchend event, then the Touchend event of the element is triggered, causing the error to occur.

Workaround

The workaround is simple, which is to stop the Touchend event bubbling when the page scrolls, which prevents the triggering of the Touchend event.

How to use

The function is introduced and called.

function Stoptouchendpropagationafterscroll() {

var locked = false;

Window.addeventlistener (' touchmove ', function(EV) {

locked | | (Locked = true, Window.addeventlistener ( ' Touchend ', Stoptouchendpropagation, true));

true);

function Span class= "Hljs-title" >stoptouchendpropagation (EV) {

Ev.stoppropagation ();

Window.removeeventlistener ( ' Touchend ', Stoptouchendpropagation, true);

locked = FALSE;

}

}

Another explanation

On the mobile side, the scroll event is triggered only once after scrolling, while the Touchmove event is triggered multiple times during the swipe process, and using scroll can be a performance optimization over the use of touchmove. However, the above code does not use the scroll event, and the Touchmove event is intended to be applied to a page less than a screen height, so it is a last resort touchmove.

Fixed Touchend event not triggered after mobile page scrolling

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.