繁体   English   中英

当移动菜单打开时,我设法禁用了身体滚动,但是当我打开移动菜单时,桌面/正文页面总是回到顶部

[英]I manage to disable scroll on body when menu mobile is open , but desktop / body page always going back to the top when i open menu mobile

我设法在身体上禁用滚动,没问题,多亏了 SO! 但是当我打开我的移动菜单时,身体总是回到顶部。

如果我删除

溢出:隐藏

滚动不再在 body 上禁用,但是当我打开我的移动菜单时,body 不会回到顶部。

当 burgermenu 打开时,我的 css 类 .overflowHidden 被添加到 body 和 html 上。

    const [showBurgerMenu, setShowBurgerMenu] = useState(false)


const handleOnClick = () => {
    const burger = document.querySelector('.burger');
    burger.classList.toggle('active');
    setShowBurgerMenu(!showBurgerMenu);
    if (showBurgerMenu === false) {
        document.querySelector("body").classList.add("overflowHidden");
        document.querySelector("html").classList.add("overflowHidden")
    } else if (showBurgerMenu === true) {
        document.querySelector("body").classList.remove("overflowHidden");
        document.querySelector("html").classList.remove("overflowHidden");
    };
}

我的 CSS 类

.overflowHidden {
  overflow: hidden;
  margin: 0;
  touch-action: none;
  -ms-touch-action: none;
  position: fixed;
  height: 100%;
}

感谢您的帮助 PS:我在 nextJS 不知道这是否重要

申请position : fixed; 然后返回您将重置身体的位置,因为固定元素不是页面流的一部分

然后我们必须将它的高度从 100% 更改为 100vh,以便元素(在本例中为主体)的高度占据整个屏幕并防止出现任何滚动条,因为我们定义了高度。

.overflowHidden {
  overflow: hidden;
  margin: 0;
  touch-action: none;
  -ms-touch-action: none;
  /* position: fixed; we get rid of this line which resets the flow of the page */
  /* height: 100%; we change 100% to 100vh */ 
  height: 100vh;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM