简体   繁体   English

将 position: fixed 应用于 position: 绝对父容器内的元素

[英]Applying position: fixed, to elements inside of a position: absolute parent container

I have a div which functions as an overlay over the body , when the overlay is active, scrolling on the document body is disabled, but any overflow on the overlay is set to overflow-y: scroll .我有一个 div 用作body上的覆盖层,当覆盖层处于活动状态时,禁用文档主体上的滚动,但覆盖层上的任何溢出都设置为overflow-y: scroll

.showComments {
  animation: show-comments 250ms forwards;
  height: 100vh;
  width: 100vw;
  background-color: black;
  z-index: 1;
  position: absolute;
  overflow-y: scroll;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

.showComments is my parent container. .showComments是我的父容器。 Inside of it, I have a nav child, I would like to apply a position: fixed to my nav , so that it stays flush on the top of the page during any user scrolling.在它里面,我有一个nav子项,我想应用一个position: fixed到我的nav ,以便它在任何用户滚动期间保持在页面顶部。 Is this possible to implement or are there any workarounds?这是否可以实施或是否有任何解决方法? Not sure if helpful but also added my CSS for the nav不确定是否有帮助,但也为nav添加了我的 CSS

NAV资产净值

.nav {
  border-bottom: $nav-border;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem;
  color: $color-white;

  h3 {
    font-family: $font-primary;
    font-weight: 600;
    font-size: 1.1rem;
  }

  svg {
    font-size: $icon-size;
    cursor: pointer;
  }
}

You can pull out the nav outside of the overlay and make it sibling as shown below.您可以拉出叠加层之外的导航并使其成为兄弟,如下所示。 Set the parent to relative and position the two children elements with absolute or fixed as needed.根据需要将父级设置为相对,position 将两个子元素设置为绝对或固定。

 .overlay-wrap{ display: relative; width: 100vw; height: 100vh; }.overlay{ position: absolute; inset: 0 0 0 0; background: black; opacity: .7; }.nav{ position: fixed; top:0; left: 0; right:0; height: 4rem; list-style:none; display: flex; background: green; z-index: 2; } li{ margin-right: 1rem; }
 <div class="overlay-wrap"> <nav class="nav"> <li>HOME</li> <li>LOGIN</li> </nav> <div class="overlay"></div> </div>

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

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