将子级扩展到父级可滚动内容的 100% 高度

Expand child to 100% height of parent scrollable content

我想在可滚动的搜索列表上显示半透明覆盖(在加载期间,更改页面后)。但我无法将覆盖扩展到完整的父内容。我无法设置固定高度,因为父项的大小可能会因结果数而异。

我试图为叠加设置 position: absolute。我也在尝试 position: sticky。最后一个效果很好,但它需要预先计算高度,滚动前需要 space。

https://jsfiddle.net/yr9xp8cs/。使用我当前的方法,叠加层 (::before) 仅具有可见部分的高度。

我想在可滚动容器的全部内容上设置覆盖,或者设置在滚动前不占用任何 space 的粘性覆盖。我不想使用 JavaScript.

这就是我要做的。为建议列表创建包装并使用覆盖。

.search-results-wrapper 具有位置样式,而 .overlay 具有 pointer-events: none 以允许其下方的列表滚动(您可以阅读完整文档 here)。

基本上:

none: The element is never the target of pointer events; however, pointer events may target its descendant elements if those descendants have pointer-events set to some other value. In these circumstances, pointer events will trigger event listeners on this parent element as appropriate on their way to/from the descendant during the event capture/bubble phases.

片段

body {
  background-color: silver;
}

.wrapper {
  position: relative;
}

.search-results-wrapper {
  position: absolute;
  top: 30px;
  left: 0;
}

.search-results {
  background-color: white;
  box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.5);
  width: 300px;
  max-height: 200px;
  overflow-y: auto;
  ul {
    padding-left: 0;
  }
  li {
    list-style: none;
    padding-left: 0;
    margin: 10px 0;
    &:nth-child(even) {
      background-color: wheat;
    }
  }
}

.overlay {
  content: '';
  display: block;
  z-index: 1;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  background: white;
  opacity: 0.7;
  pointer-events: none;
}
<div class="wrapper">
  <input type="text" placeholder="Search phrase"></input>
  <div class="search-results-wrapper">
    <div class="overlay"></div>
    <div class="search-results">
      <!-- place semi transparent overlay here -->
      <div>Suggestions:</div>
      <ul>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
        <li>Lorem ipsum</li>
      </ul>
      <div>Page 1/20</div>
    </div>
  </div>
</div>