繁体   English   中英

如果父级为“固定”位置,如何在溢出时自动添加滚动

[英]How to add scroll auto on overflow in case the parent as `fixed` position

我试图用固定元素的子元素添加溢出自动。 但不是炒锅 正确的方法是什么?

如何计算高度呢?

 .parent{ border:2px solid red; position:fixed; top:0; left:0; width:20%; height:50%; } .content{ height:100%; overflow:auto; } 
 <div class="parent"> <header> <h1>Title</h1> </header> <div class="content"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). </div> </div> 

现场演示

添加溢出:隐藏在父对象上

 .parent { border: 2px solid red; position: fixed; top: 0; left: 0; width: 20%; height: 50%; overflow: hidden; } .content { height: 100%; overflow: auto; } 
 <div class="parent"> <header> <h1>Title</h1> </header> <div class="content"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). </div> </div> 

您确实需要一个像素值作为高度。

如果您将.content高度发送为100px,则滚动确实可以正常工作。

清除overflow:auto; .content clas并将其添加到.parent类。

喜欢:

.parent {
  border:2px solid red;
  position:fixed;
  top:0;
  left:0;
  width:20%;
  height:50%;
  overflow:auto;
}

.content {
  height:100%;
}

 .parent{ position: fixed; border:2px solid red; top:0; left:0; width:20%; height:50%; } header { position: fixed; height: 80px; } .content{ height: calc( 100% - 80px ); overflow:auto; margin-top: 80px; } 
 <div class="parent"> <header> <h1>Title</h1> </header> <div class="content"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). </div> </div> 

 var element = document.querySelectorAll(".parent")[0]; alert("The height of parent div is : "+element.offsetHeight+"px"); 
 body{ margin:0; } .parent{ height:172px; } header{ border:2px solid red; position:fixed; top:0; left:0; right:0; height:80px; } .content{ height:100%; margin-top:82px; overflow:auto; border:2px solid black; } 
 <div class="parent"> <header> <h1>Title</h1> </header> <div class="content"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). </div> </div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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