[英]How to make the children in the viewport as long as the parent is visible
height: 100vh; width: 100%
在两者里面我有一个height: 100vh; width: 100%
height: 100vh; width: 100%
. height: 100vh; width: 100%
。 #parent { height: 400vh; background: green; padding: 1rem; &.2 { background: blue; } } #children { height: 100vh; background: red; }
<div id="parent"> <div id="children"> children content </div> </div> <div id="parent" style="background: blue"> <div id="children"> children content </div> </div>
But i want the children to always be in the viewport as long as the parent is displayed.但我希望孩子们总是在视口中,只要显示父母。 To be clearer, i want the children to move inside the parent to be always visible but it stops at the bottom of the parent.更清楚地说,我希望孩子们在父母内部移动以始终可见,但它停在父母的底部。
body { margin: 0; padding: 0; } #parent { background: green; height: 400vh; padding: 2.5vw; } #children { width: 90vw; height: 90vh; background: red; position: fixed; }
<div id="parent"> <div id="children"> chidren-content <div /> </div>
But I can't do that with multiple parent's because of the fixed property.但是由于固定属性,我不能对多个父母这样做。 I have tried to replace it with JS when the children is almost at the end of the parent but it does weird things.当孩子几乎在父母的末尾时,我试图用 JS 替换它,但它会做一些奇怪的事情。
So anyone have an idea to do that with multiple parents?所以有人有想法和多个父母一起做吗?
height: 100vh; width: 100%
在两者里面我有一个height: 100vh; width: 100%
height: 100vh; width: 100%
. height: 100vh; width: 100%
。 #parent { height: 400vh; background: green; padding: 1rem; &.2 { background: blue; } } #children { height: 100vh; background: red; }
<div id="parent"> <div id="children"> children content </div> </div> <div id="parent" style="background: blue"> <div id="children"> children content </div> </div>
But i want the children to always be in the viewport as long as the parent is displayed.但我希望孩子们总是在视口中,只要显示父母。 To be clearer, i want the children to move inside the parent to be always visible but it stops at the bottom of the parent.更清楚地说,我希望孩子们在父母内部移动以始终可见,但它停在父母的底部。
body { margin: 0; padding: 0; } #parent { background: green; height: 400vh; padding: 2.5vw; } #children { width: 90vw; height: 90vh; background: red; position: fixed; }
<div id="parent"> <div id="children"> chidren-content <div /> </div>
But I can't do that with multiple parent's because of the fixed property.但是由于固定属性,我不能对多个父母这样做。 I have tried to replace it with JS when the children is almost at the end of the parent but it does weird things.当孩子几乎在父母的末尾时,我试图用 JS 替换它,但它会做一些奇怪的事情。
So anyone have an idea to do that with multiple parents?所以有人有想法和多个父母一起做吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.