最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论