Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable touch action for on single HTML Element in iOS

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.

like image 594
Raja Avatar asked Apr 09 '18 05:04

Raja


People also ask

How do I turn off touch events in CSS?

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.

What is touch-action None?

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).

What is touch-action manipulation?

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).

Which method is used from touch-action to scroll on the touch screen using finger based motion events?

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.


2 Answers

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 .

like image 62
Chirag Ravindra Avatar answered Sep 28 '22 06:09

Chirag Ravindra


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

like image 33
Denno Avatar answered Sep 28 '22 06:09

Denno