Briefly discuss the mobile Touch event processing several pits, and the corresponding simple processing method.
Click through
Suppose there is a pop-up layer with a closed button on it that supports touchend, and if there is an element below that supports the Click event, the Click event is triggered on the lower element after the popup layer is closed. This effect is definitely not what we need, and we can't be sure that a touch-enabled pop-up will appear above, so I think the best way to do this is to disable the click event for all elements, and use touchend for a better experience than the click requires up to 1s of trigger time.
Determination of TAP Events
A correct tap event should satisfy the condition:
- Triggered when the user's finger moves away from the screen
- Cannot be triggered when the user moves the finger (prevents and scrolls, drags the event to conflict)
- Cannot trigger when multiple fingers touch the screen at the same time
- The Click event should not be triggered
Specific implementation code can refer to Tap-event.
Using native scrolling events
Android 4.0 below is not supported for native webview scrolling, so only tools like iscroll can be used to simulate element scrolling. The downside is that it's a bit too complex, so I'm going to use the native scroll if the conditions permit.
Enabling native scrolling only requires styling the outer element -webkit-overflow-scrolling: touch; , and if your listener function is more resource-intensive we can limit its trigger interval by a simple buffer function, for example:
function buffer(fn, ms) { var timeout; return function() { if (timeout) return; var args = arguments; timeout = setTimeout(function() { timeout = null; fn.apply(null, args); }, ms); }}document.querySelector(‘.scrollable‘).onscroll = buffer(onScroll, 100);
It is also recommended that you do not use shadow styles (Text-shadow and Box-shadow) on scrollable elements, because they affect performance very much and do not look very attractive.
Disable page Overall drag
By default, the user's drag operation causes the overall page to scroll after the scroll rolls to the head, one way is to disable the Touchmove native trigger of the document.
‘touchmove‘, function (e) { e.preventDefault();});
The native scroll is not working at this time, and the workaround is to disable the Touchmove event bubbling of the scrolling element
‘touchmove‘, function (e) { e.stopPropagation();}
Another way is to determine that scrolling elements are disabled by default after rolling to the head
var el = Document.queryselector ( Span class= "string" > scrollable '); var sy = 0;events.bind (el, ' Touchstart ', function (e) {sy = e.pagey;}) Events.bind (el, ' Touchmove ', function (e) {var down = (E.pagey-sy > 0); //top if (down && el.scrolltop <= 0) { E.preventdefault (); } //bottom if (!down && el.scrolltop >= el.scrollheight- El.clientheight) {E.preventdefault ()}})
I personally prefer the second scenario, because if you simply disable the Touchmove monitoring of document, it will result in some processing failure, such as the above mentioned tap-event module.
Drag direction and distance
The PageX and Pagey properties of the event can be calculated,
Touch event handling on the mobile side