修复可滚动div内的元素

时间:2017-12-03 20:20:23

标签: javascript html css

.parent div有overflow: scroll.childposition: fixed。我想滚动.parent div的内容,同时保留.child在其中的位置。

.parent {
  border: 1px;
  height: 500px;
  width: 900px;
  overflow: scroll;
}

.child {    
  background: black;
  color: white;
  position: fixed;    
  height: 100px;
  width: 900px;
}
<div class="parent">
  parent
  <div class="child">
    child
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
.parent {
height:500px;
width:500px;
overflow:scroll;
position:absolute;
background:black;
}


.child {

position: sticky;
top:0;
height:80px;
width:500px;
background-color: #cae8ca;
}
&#13;
<div class = "parent" >
<div class = "child"  >
chi
</div>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
<hr>
<br>
</div>
&#13;
&#13;
&#13;