I need to disable touch action on html element for some reason. I have tired with CSS property touch-action: none. But it is not working for Safari and iOS devices. Is there any way to disable touch action for html elements on iOS.
For HTML you can use the followings: none – disable all of the pointer and touch events, auto – set back the default value, inherit – inherit the value from its parent.
By default, a browser will handle touch interactions automatically: Pinch to zoom, swipe to scroll, etc. Setting touch-action to none will disable all browser handling of these events, leaving them up to you to implement (via JavaScript).
The touch-action CSS property sets how an element's region can be manipulated by a touchscreen user (for example, by zooming features built into the browser).
A swipe gesture generates scroll events in addition to the swipe event. Depending on the length of the swipe, it is possible that the swipe and scroll events have different targets. The target of a scroll event is the node at the point where the gesture started.
If you don't mind click events and other pointer event also being stopped, you can try pointer-events:none;
in your CSS. If you want this to be specific to mobile devices, you can apply it using media queries. If you want it only on iOS you can do user agent sniffing in JS [As done in @Denno's Answer] and apply a class with that style on it to your element .
Whilst it's not recommended, you can use user agent sniffing to determine if a user is on an iOS device
var is_iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
Then, using that in an if condition, create an event listener to listen for touch events, and then prevent their default action
if (is_iOS) {
document.querySelector('.some-element').addEventListener('touchstart touchmove touchend', function (e) {
e.preventDefault();
});
}
I haven't tested the above code, but in theory it should work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With