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

时间:2018-06-25 10:36:50

标签: javascript html css

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

如何计算高度?

.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>  

Live Demo

5 个答案:

答案 0 :(得分:1)

添加溢出:隐藏在父项上

.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>

答案 1 :(得分:0)

您真的需要一个像素值作为高度。

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

答案 2 :(得分:0)

overflow:auto;类别中删除.content,并将其添加到.parent类中。

赞:

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

.content {
  height:100%;
}

答案 3 :(得分:0)

.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>

答案 4 :(得分:0)

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>