使标题在可滚动的侧边栏中粘贴

时间:2016-04-06 18:57:15

标签: html css css3

我有一个带标题的侧栏。有时内容会溢出,所以我在整个元素上使用overflow-y: scroll。但是当我滚动时,现在标题(带有关闭动作)移出视图。当内容滚动时,我希望标题保持不变。

有没有办法只使用CSS?

Pen

*, *:after, *:before {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
html, body {
  height: 100%;
  font-family: 'Open Sans', sans-serif;
}
html {
  font-size: 10px;
  -webkit-tap-highlight-color: transparent;
}
body {
  color: #999;
  font-size: 14px;
  line-height: 20px;
}
#side-panel.presented {
  right: 0;
}
#side-panel {
  border-left: none;
  bottom: 0;
  position: fixed;
  right: -400px;
  top: 60px;
  width: 400px;
  overflow-y: scroll;
  -moz-box-shadow: -2px 0 5px #dfdfdf;
  -webkit-box-shadow: -2px 0 5px #dfdfdf;
  box-shadow: -2px 0 5px #dfdfdf;
}
<div id="side-panel" class="ember-view presented">
  <div class="side-panel-header">
    <h2>My header</h2>
  </div>
  <div class="side-panel-body">
    <div class="timeline">
      <ol>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">last</div></li>
      </ol>
    </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:1)

如果您移动.side-panel-body上的可滚动内容并设置小于容器的height,请为标题留出足够的空间。应该这样做。

<强> jsFiddle

&#13;
&#13;
*, *:before, *:after {
  box-sizing: border-box;
}
#side-panel {
  position: fixed;
  bottom: 0;
  right: 0;
  width: 400px;
  height: 200px;
  border: 1px solid;
}
.side-panel-body {
  width: 100%;
  height: 100px;
  overflow: auto;
}
&#13;
<div id="side-panel" class="ember-view presented">
  <div class="side-panel-header">
    <h2>My header</h2>
  </div>
  <div class="side-panel-body">
    <div class="timeline">
      <ol>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">thing</div></li>
        <li><div class="type" data-type="milestone">last</div></li>
      </ol>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

尝试添加此内容:

&#13;
&#13;
.side-panel-header{
  position: fixed;
  background-color: white;
  height: 50px;
}

.side-panel-body{
  padding-top: 35px;
}
&#13;
&#13;
&#13;

这就是你想要的吗?

另一种选择是你将溢出属性赋予身体

&#13;
&#13;
.side-panel-body{
  overflow-y: scroll;
  height: 100%;
}
&#13;
&#13;
&#13;