javascript - 固定页眉/页脚将垂直滚动条保持在顶部?

标签 javascript jquery html css

我有一个部分透明的固定页脚和带有滚动内容的页眉:https://jsfiddle.net/ru8jgxg9/

需要对 JSFiddle 进行哪些更改才能在内容溢出时将垂直滚动条保持在顶部(但也要将滚动条保持在窗口的整个高度)?

我注意到 stackoverflow.com 似乎能够做到这一点:

enter image description here

enter image description here

html {
  height: 100%;
}

body {
  height: 100%;
}

/* Fixed Header */
.dvTableTop {
  display: table;
  width: 100%;
  border-style: solid;
  border-width: 0px 0px 2px 0px;
  border-color: #000000;
  top: 0px;
  height: 50px;
  position: fixed;
  left: 0;
  right: 0;
  opacity: 0.7;
  background-color: Red;
  z-index: 1030;
}

/* Scrollable Content */
.dvContentContainer1 {
  position: fixed;
  top: 0;
  bottom: 0;
  padding-top: 30px;
  overflow: auto;
  left: 0;
  right: 0;
}


/* Fixed Footer */
.dvFooterContainer1 {
  position: fixed;
  height: 50px;
  background-color: Yellow;
  bottom: 0;
  left: 0;
  right: 0;
  opacity: 0.7;
}

最佳答案

您的固定页眉和页脚需要位于滚动容器内。目前,它们位于内容容器之外,并将与内容容器及其滚动条重叠。

另外,你的内容容器不能有position: fixed,否则会和其他固定元素争夺位置,造成重叠。固定元素总是相对于文档,而不是容器。

下面是一个工作示例。

body {
  margin: 0;
  padding: 0;
  
  font-family: arial, helvetica, san-serif;
}

.content {
  height: 1000px;
  
  background: linear-gradient(45deg, blue, red);
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 50px;
  
  background: rgba(0, 255, 0, 0.5);
}

.footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  
  background: rgba(0, 255, 0, 0.5);
}
<div class="content">
  <div class="header">Header</div>
  <div class="footer">Header</div>
</div>

关于javascript - 固定页眉/页脚将垂直滚动条保持在顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45363127/

相关文章:

javascript - 如何以编程方式在 Protractor 中设置下载路径?

javascript - ng-click 不会触发 $mdDialog 回调

jquery - 根据其背景图像高度响应地调整 div 的大小

php - 是否可以使用 jQuery 加密 AJAX 调用以进行身份​​验证?

javascript - JQuery UI - 可删除或排序 "clear"绝对位置元素彼此靠近

javascript - Meteor.call 回调函数返回未定义

javascript - 单击按钮时显示加载器

javascript - 尝试在这个特定的 javascript 函数中传递函数参数

html - 使左对齐的div占据所有空间

javascript - 为什么这段 jQuery 代码返回一个数组而不是单个属性?