绝对定位的div div滚动内绝对定位的父
我有一个绝对定位div与两个孩子 - 一个绝对定位的div和静态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;
}
我不得不受限于母公司的边缘绝对的孩子,所以为何仍滚动,我怎样才能使它保持放?
我通过把我想在一个绝对定位的div与overflow-y: scroll
滚动,像这样的元素解析造型如下:
.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;
}
看起来你已经用额外的标记排序,类似于我的答案中链接的问题。不错的工作! – TylerH
你应该调整你的孩子div来使用position: fixed
,如果你不希望它移动。 position: absolute
只是告诉一个div,它的最初的位置应该是绝对确定的。查看我的回答here了解更多有关position: fixed
的信息以及与您相似的场景。
设置.frame
div为position: relative
(或其父母.frame
)以使其生效。这会将position: fixed
孩子设置在.frame
的父母position: relative
内。
您将需要调整定位数量(顶部,底部,左侧,右侧)以考虑不同的堆叠环境。
事情是这样的:
<div class='frame'>
<div class='fix-me'></div>
<div class='scroll-me'>
<div class='long-content'></div>
</div>
</div>
: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'中的元素,并修改'.absolute-content'内的绿色'.frame' –
@DustinRichardLocke从您的演示中.frame是文档中唯一的东西。我稍后回家时会进行更新;在演示中添加一些周围的现实元素也会对我有所帮助。 – TylerH
你试过'的位置是:固定;'了吗? –
是的。但是我想让它在父窗口内而不是整个窗口内修复。 –