阻止固定导航菜单下的滚动

huangapple go评论74阅读模式
英文:

Prevent Scrolling Beneath Fixed Nav Menu

问题

我已经寻找了一段时间解决我的问题的方法,可以找到许多与类似问题相关的文章,但没有完全解决我的问题。

我有一个用React编写的应用程序。在移动网站的版本上,用户可以点击汉堡菜单以打开导航菜单。导航菜单占据了80vw100vh。由于某种原因,用户仍然可以通过滑动导航菜单来滚动应用程序的背景。

我的移动导航样式如下。

.mobileNav {
  height: 100vh;
  width: 0vw;
  position: fixed;
  top: 0;
  right: 0;
  background-color: #1d1d1d;
  z-index: 999;
  will-change: transform;

  -webkit-transition: 0.75s;
  -moz-transition: 0.75s;
  -ms-transition: 0.75s;
  -o-transition: 0.75s;
  transition: width 0.75s;

  &--open {
    width: 80vw;
  }
}

我尝试过在打开导航栏时将position: fixedoverflow: hidden设置为父元素。这可以防止滚动,但也会将用户跳到页面顶部,这是不希望的。

这是我的jsfiddle链接https://codesandbox.io/s/react-fiddle-forked-p9fqc8?file=/src/App.js&resolutionWidth=492&resolutionHeight=675

英文:

I've been looking for a solution for my problem for a bit and I can find many articles for similar issues, but nothing that quite solves my problem.

I have an app written in React. On the mobile version of the site users can click a hamburger menu to open a navigation menu. The nav menu takes up 80vw and 100vh. For some reason users are still able to scroll the application in the background by swiping the nav menu.

My mobile nav is styled as follows.

.mobileNav {
  height: 100vh;
  width: 0vw;
  position: fixed;
  top: 0;
  right: 0;
  background-color: #1d1d1d;
  z-index: 999;
  will-change: transform;

  -webkit-transition: 0.75s;
  -moz-transition: 0.75s;
  -ms-transition: 0.75s;
  -o-transition: 0.75s;
  transition: width 0.75s;

  &--open {
    width: 80vw;
  }
}

I have tried setting position: fixed or overflow: hidden to the parent element when the navBar is opened. This will prevent scrolling but also jumps the user to the top of the page which is undesirable.

Here is my jsfiddle https://codesandbox.io/s/react-fiddle-forked-p9fqc8?file=/src/App.js&resolutionWidth=492&resolutionHeight=675.

答案1

得分: 0

这实际上是默认行为。您需要做的是编写一些代码,使在模态框打开时背景不可滚动。以下是可能有所帮助的快速阅读链接:
https://css-tricks.com/prevent-page-scrolling-when-a-modal-is-open/

英文:

Hi this is actually default behavior. What you have to do is write some code that makes the background unscrollable when the modal is open. Here is a quick read that might help:
https://css-tricks.com/prevent-page-scrolling-when-a-modal-is-open/

答案2

得分: 0

const App = () => {
  const [isNavOpen, setIsNavOpen] = useState(false);

  useEffect(() => {
    document.body.style.overflow = isNavOpen ? "hidden" : "auto";
  }, [isNavOpen]);

  return (
    <div className="App">
      <NavMenu isNavOpen={isNavOpen} setIsNavOpen={setIsNavOpen} />
      <div className={styles.red}>
        Hello <button onClick={() => setIsNavOpen(true)}>Open</button>
      </div>
      <div className={styles.blue}>Hello</div>
      <div className={styles.green}>Hello</div>
    </div>
  );
};

export default App;

useEffect will fire every time the nav modal swaps between being open / closed. when it's set to closed, we enable body scrolling. when it's open, we disable body scrolling.


<details>
<summary>英文:</summary>

const App = () => {
const [isNavOpen, setIsNavOpen] = useState(false);

useEffect(() => {
document.body.style.overflow = isNavOpen ? "hidden" : "auto";
}, [isNavOpen]);

return (
<div className="App">
<NavMenu isNavOpen={isNavOpen} setIsNavOpen={setIsNavOpen} />
<div className={styles.red}>
Hello <button onClick={() => setIsNavOpen(true)}>Open</button>
</div>
<div className={styles.blue}>Hello</div>
<div className={styles.green}>Hello</div>
</div>
);
};

export default App;


useEffect will fire every time the nav modal swaps between being open / closed. when it&#39;s set to closed, we enable body scrolling. when it&#39;s open, we disable body scrolling.

</details>



huangapple
  • 本文由 发表于 2023年7月31日 21:15:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定