【问题标题】:Position sticky for sidebar whick is larger than screen size大于屏幕尺寸的侧边栏位置置顶
【发布时间】:2021-11-23 05:42:16
【问题描述】:

我有一个高度大于屏幕尺寸的侧边栏,当使用position: sticky 时,在滚动到页面末尾之前,我将无法看到侧边栏的底部。 如何确保在滚动时,例如 50% 的页面已经显示侧边栏的下部?

这里的代码示例:https://jsfiddle.net/zxypmqvg/3/

.main-block {
  display: flex;
}

.sidebar {
  background-color: LightCoral;
  width: 25%;
}

.sidebar-content {
  margin: 5px;
  margin-bottom: 40px;
  background-color: LimeGreen;
  position: sticky;
  top: 0;
}

.main-content {
  width: 75%;
  height: 4000px;
  background-color: LightBlue;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>

<body>
  <div class="main-block">
    <div class="sidebar">
      <div class="sidebar-content">
        <p>sometext for example for stackoverfrom</p>
        <p>sometext for example for stackoverfrom</p>
        <p>sometext for example for stackoverfrom</p>
        <p>sometext for example for stackoverfrom</p>
        <p>sometext for example for stackoverfrom</p>
        <p>sometext for example for stackoverfrom</p>
        <p>last string</p>
      </div>
    </div>
    <div class="main-content">

    </div>
  </div>
</body>

</html>

最后一个字符串仅在达到页尾时显示

【问题讨论】:

    标签: css css-position sticky


    【解决方案1】:

    也许,一个好的做法是将这个 css 添加到 .sidebar-content

    overflow-y: auto;
    max-height: 100vh;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 2022-10-06
      相关资源
      最近更新 更多