【问题标题】:Absolute-positioned div scrolls inside absolute-positioned parent绝对定位的 div 在绝对定位的父级内滚动
【发布时间】:2017-09-06 07:06:24
【问题描述】:

我有一个绝对定位的 div 和两个孩子——一个绝对定位的 div 和一个将在父级内滚动的静态 div。它看起来像这样:

<div class='frame'>
  <div class='absolute-contents'>This should stay put.</div>
  <div class='static-contents'>This should scroll under it.</div>
</div>

这是 CSS:

.frame {
  position: absolute;
  top: 40px;
  left: 40px;
  right: 40px;
  bottom: 40px;
  overflow-y: scroll;
}

.absolute-contents {
  position: absolute;
  top: 40px;
  left: 40px;
  right: 40px;
  bottom: 40px;
  z-index: 9999;
  opacity: .9;
  padding: 40px;
}

.static-contents {
  margin: 24px auto;
  width: 400px;
  height: 3000px;
  padding: 40px;
}

我的绝对子级被限制在父级的边缘,为什么它仍然滚动,我怎样才能让它保持原样?

示例:https://codepen.io/anon/pen/wqZxXG

【问题讨论】:

  • 你试过position: fixed;了吗?
  • 是的。但我希望它固定在父窗口中,而不是整个窗口。

标签: html css


【解决方案1】:

我通过将我想要滚动的元素放在带有overflow-y: scroll 的绝对定位的 div 中来解决,如下所示:

<div class='frame'>
  <div class='fix-me'></div>
  <div class='scroll-me'>
    <div class='long-content'></div>
  </div>
</div>

还有这样的样式:

.frame {
  background-color: blue;
  position: absolute;
  top: 40px;
  right: 40px;
  left: 40px;
  bottom: 40px;
  overflow-y: hidden;
}

.scroll-me {
  background-color: orange;
  position: absolute;
  top: 40px;
  right: 40px;
  left: 40px;
  bottom: 40px;
  overflow-y: scroll;
}

.fix-me {
  position: absolute;
  z-index: 999;
  top: 40px;
  left: 40px;
  right: 40px;
  height: 56px;
  background-color: purple;
}

.long-content {
  width: 480px;
  background-color: yellow;
  height: 4000px;
  margin: 20px auto;
}

笔在这里:https://codepen.io/dustinlocke/pen/vJMzpK

【讨论】:

  • 看起来你用额外的标记对其进行了排序,类似于我的答案中链接的问题。干得好!
【解决方案2】:

如果您不希望它移动,您应该调整您的子 div 以使用 position: fixedposition: absolute 只是告诉 div 它的 initial 位置应该绝对确定。有关position: fixed 的更多信息以及与您的类似情况,请参阅我的回答here

.frame div 设置为position: relative(或.frame 的父级)以使其正常工作。这会将position: fixed 子级设置为固定在.frameposition: relative 父级内。

您需要调整定位量(上、下、左、右)以适应不同的堆叠环境。

类似这样的:https://codepen.io/anon/pen/brJxVW

body {
  width: 100vw;
}

.frame {
  background-color: green;
  position: relative;
  width: calc(100vw - 80px);
  margin: 0 auto;
  top: 40px;
  bottom: 40px;
  overflow-y: scroll;
}

.absolute-contents {
  background-color: yellow;
  position: fixed;
  top: 40px;
  left: 40px;
  right: 40px;
  bottom: 40px;
  z-index: 9999;
  opacity: .9;
  margin: 40px;
}

.big-contents {
  margin: 24px auto;
  width: 400px;
  height: 3000px;
  background-color: white;
  padding: 40px;
}
<div class='frame'>
    <div class='absolute-contents'>This should stay fixed in the green frame. Why is it scrolling?</div>
    <div class='big-contents'>This should scroll under it.</div>
</div>

【讨论】:

  • 您的笔将子 div 固定在整个窗口内,而不仅仅是 .frame 元素并滚动整个正文。我只想滚动绿色 .frame 内的元素,并修复绿色 .frame 内的 .absolute-content
  • @DustinRichardLocke 在您的演示中,.frame 是文档中唯一的内容。稍后我回家时会更新;在你的演示中添加一些周围的现实元素也会对我有所帮助。
猜你喜欢
  • 2017-01-16
  • 1970-01-01
  • 1970-01-01
  • 2014-03-22
  • 2018-12-05
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
相关资源
最近更新 更多