admin管理员组文章数量:1425760
I have a ponent in react on which touchmove
event should be disabled. I have tried the following but it does not work.
parentRef.current.addEventListener("touchMove", e => e.preventDefault(), false)
I have a ponent in react on which touchmove
event should be disabled. I have tried the following but it does not work.
parentRef.current.addEventListener("touchMove", e => e.preventDefault(), false)
Share
Improve this question
edited May 21, 2022 at 23:43
RBT
26k24 gold badges175 silver badges260 bronze badges
asked May 21, 2022 at 10:21
akshadaakshada
391 silver badge3 bronze badges
1
- 2 Does this answer your question? How to prevent default handling of touch events? – Cristik Commented May 24, 2022 at 5:09
3 Answers
Reset to default 4You can simply use the touch-action property in your CSS file to remove the scroll event from your html body or an element. Add the below code in your code.
touch-action: none;
-ms-touch-action: none;
You can check if this device has innerWidth below certain pixels then set overflow:hidden
and height & width to 100vh
& 100vw
respectively to the parentRef in useEffect
To prevent scrolling using CSS on React rendered ponents, we can set the overflow CSS property to hidden with JavaScript.
For instance, we write:
import React, { useEffect } from "react";
export default function App() {
useEffect(() => {
document.body.style.overflow = "hidden";
}, []);
return <div>hello world</div>;
}
to set the overflow CSS of the body element to hidden when the ponent mounts with:
document.body.style.overflow = "hidden";
The useEffect callback only runs when the ponent mounts since we passed in an empty array as the 2nd argument of useEffect.
本文标签: javascriptHow to disable scroll on touchStack Overflow
版权声明:本文标题:javascript - How to disable scroll on touch? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745449823a2658832.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论