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