Div 固定 header,悬停在 header 上时内容可滚动

Div with fixed header, content scrollable while hovered over header

我有一个可滚动的内容 div,顶部有一个固定的 header。问题是当用户将鼠标悬停在 header 上并尝试滚动时,内容不会滚动。如何将 header 固定在顶部,同时能够在悬停 header 时滚动内容?

body {
        margin: 0;
    }
    .wrapper {
        width: 60%;
    }
    .header {
        position: fixed;
        top: 0;
        width: calc(60% - 2em);
        padding: 1em;
        display: flex;
        justify-content: space-between;
        align-items: center;
        background-color: #e5e5e5;
    }
    .content {
        max-height: 300px;
        overflow-y: auto;
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
        padding-top: 3.5em;
    }
    a.tile {
        width: calc(33.333% - 32px);
        height: 200px;
        margin-bottom: 6px;
        padding: 12px;
        text-align: center;
        border-radius: 12px;
        border: 1px solid #efefef;
    }
<body>
    <div class="wrapper">
        <div class="header">
            <span>9 of 25</span>
            <div class="links">
                <a href="#" disabled>Previous</a>
                <a href="#">Next</a>
            </div>
        </div>
        <div class="content">
            <a href="#" class="tile">Tile 1</a>
            <a href="#" class="tile">Tile 2</a>
            <a href="#" class="tile">Tile 3</a>
            <a href="#" class="tile">Tile 4</a>
            <a href="#" class="tile">Tile 5</a>
            <a href="#" class="tile">Tile 6</a>
            <a href="#" class="tile">Tile 7</a>
            <a href="#" class="tile">Tile 8</a>
            <a href="#" class="tile">Tile 9</a>
        </div>
    </div>
</body>

您可以在示例中看到,当悬停在 header 上时 body 会滚动,但内容 div 不会滚动。在全屏视图中可以更好地看到问题。

当光标在 header 上时滚动不起作用的原因是因为 header 元素不是可滚动元素的 child:因此滚动事件将不工作。

如果允许您更改 DOM,我建议将 header 移到溢出容器中:这样滚动事件会自动处理。此外,在其上使用 position: sticky 将确保浏览器为元素保留足够的 space,而无需定义顶部填充:参见下面的 proof-of-concept:

* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

.wrapper {
  width: 60%;
}

.header {
  position: sticky;
  top: 0;
  width: 100%;
  padding: 1em;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #e5e5e5;
}

.content {
  max-height: 300px;
  overflow-y: auto;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}

a.tile {
  width: calc(33.333% - 32px);
  height: 200px;
  margin-bottom: 6px;
  padding: 12px;
  text-align: center;
  border-radius: 12px;
  border: 1px solid #efefef;
}
<body>
  <div class="wrapper">
    <div class="content">
      <div class="header">
        <span>9 of 25</span>
        <div class="links">
          <a href="#" disabled>Previous</a>
          <a href="#">Next</a>
        </div>
      </div>
      <a href="#" class="tile">Tile 1</a>
      <a href="#" class="tile">Tile 2</a>
      <a href="#" class="tile">Tile 3</a>
      <a href="#" class="tile">Tile 4</a>
      <a href="#" class="tile">Tile 5</a>
      <a href="#" class="tile">Tile 6</a>
      <a href="#" class="tile">Tile 7</a>
      <a href="#" class="tile">Tile 8</a>
      <a href="#" class="tile">Tile 9</a>
    </div>
  </div>
</body>