javascript - How to disable scroll on touch? - Stack Overflow

I have a ponent in react on which touchmove event should be disabled. I have tried the following but it

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
Add a ment  | 

3 Answers 3

Reset to default 4

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745449824a4628216.html

相关推荐

  • javascript - How to disable scroll on touch? - Stack Overflow

    I have a ponent in react on which touchmove event should be disabled. I have tried the following but it

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信