Fixed Touchend event not triggered after mobile page scrolling

Source: Internet
Author: User

Resolves a touchend event issue when a mobile page scroll is not triggered

When you optimize on a mobile page, you typically use touch events instead of mouse-related events. Use the Touchend event instead of the PC-side click and MouseUp events.

However, the Touchend event has a problem when scrolling through the page. After scrolling, assuming that 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 very easy. is to stop the Touchend event bubbling when the page scrolls. This prevents Touchend events from being triggered.

Usage

The function is introduced and called.

 function stoptouchendpropagationafterscroll(){    varLocked =false; Window.addeventlistener (' Touchmove ', function(EV){Locked | | (Locked =true, Window.addeventlistener (' Touchend ', Stoptouchendpropagation,true)); },true); function 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 the scroll has ended. The Touchmove event is triggered several times during the sliding process. Using scroll can be a performance optimization over the use of touchmove.

But. The above code does not use the scroll event, but the Touchmove event. is to apply to pages that are smaller than one screen height at the same time. So I was forced to use 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.