One reason for browser interference with gestures is to facilitate page scrolling and updates. To counteract this, you can include touch-action: none;
in your CSS for the current element before any pointer events occur.
Alternatively, utilizing touch events
(instead of pointer events) with passive: false
and event.preventDefault()
immediately (outside of a promise) can prevent browsers from intercepting gestures after a pointer event occurs. Ensure that this listener is added first in order to take precedence over other event listeners.
const onTouchMove = (ev:TouchEvent)=>{
ev.preventDefault()
}
element.addEventListener(
'touchmove',
onTouchMove,
{ passive: false }
)
Despite these methods, there may still be issues with the pointerleave
event not functioning correctly when moving a finger outside of an element compared to using a mouse.
To address this issue and enable functions such as pointerleave
, adjustments can be made within the pointerdown
event:
const onPointerDown = function (ev) {
ev.target.releasePointerCapture(ev.pointerId)
}
element.addEventListener(
'pointerdown',
onPointerDown
)
Additionally, CSS offers useful properties like user-select: none;
(which prevents selection and is inherited) and pointer-events: none;
(which renders an element transparent to pointer events and is also inherited).