如何触摸滚动?

发布于 2025-01-31 04:07:53 字数 170 浏览 1 评论 0原文

我有一个组件,其中应禁用touchmove事件。我尝试了以下内容,但它不起作用。

parentRef.current.addEventListener("touchMove", e => e.preventDefault(), false)

I have a component 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奢华的一滴泪 2025-02-07 04:07:53

您可以简单地使用 touch-action> touch-action 您的CSS文件以从HTML主体或元素中删除滚动事件。在您的代码中添加以下代码。

touch-action: none;
-ms-touch-action: none;

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;
青瓷清茶倾城歌 2025-02-07 04:07:53

您可以检查此设备是否在某些像素以下的内部宽度,然后设置溢出:隐藏和height&宽度到100VH& 100VW分别在使用effect的parentref

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

旧时模样 2025-02-07 04:07:53

为了防止在React渲染的组件上使用CSS滚动,我们可以将Overflow CSS属性设置为使用JavaScript隐藏。

例如,我们写入:

import React, { useEffect } from "react";

export default function App() {
  useEffect(() => {
    document.body.style.overflow = "hidden";
  }, []);

  return <div>hello world</div>;
}

将车身元素的溢出CS设置为隐藏时,当组件安装时:

document.body.style.overflow = "hidden";

使用效率回调仅在组件安装时才运行,因为我们以空数组作为使用效果的第二个参数。

To prevent scrolling using CSS on React rendered components, 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 component mounts with:

document.body.style.overflow = "hidden";

The useEffect callback only runs when the component mounts since we passed in an empty array as the 2nd argument of useEffect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文