简体   繁体   English

只要父级可见,如何使子级在视口中

[英]How to make the children in the viewport as long as the parent is visible

I have 2 similar parent with a height of 400vh.我有 2 个相似的父母,身高为 400vh。 And inside of both I have a children of height: 100vh; width: 100%在两者里面我有一个height: 100vh; width: 100% height: 100vh; width: 100% . height: 100vh; width: 100%

This is an example这是一个例子

 #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.更清楚地说,我希望孩子们在父母内部移动以始终可见,但它停在父母的底部。

This is an example of how I want it to work这是我希望它如何工作的一个例子

 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?所以有人有想法和多个父母一起做吗?

I have 2 similar parent with a height of 400vh.我有 2 个相似的父母,身高为 400vh。 And inside of both I have a children of height: 100vh; width: 100%在两者里面我有一个height: 100vh; width: 100% height: 100vh; width: 100% . height: 100vh; width: 100%

This is an example这是一个例子

 #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.更清楚地说,我希望孩子们在父母内部移动以始终可见,但它停在父母的底部。

This is an example of how I want it to work这是我希望它如何工作的一个例子

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM