javascript - 如何在不使用javascript的情况下使内容div可滚动

标签 javascript html css

我有一个简单的页面。在这里,我想要一个固定的页眉和固定的页脚,并且内容正文可以滚动。

HTML

<div class="container">
    <div class="header">This is Header</div>
    <div class="body">This is Body
        <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body</div>
    <div class="footer">This is Footer</div>
</div>

CSS

* {
    margin: 0;
    padding: 0;
}
.container {
    width: 100%;
    height: 100%;
    background: #DDD;
}

.header {
    width: 100%;
    height: 40px;
    background: red;
    position: absolute;
    top: 0;
}

.footer {
    width: 100%;
    height: 40px;
    background: green;
    position: absolute;
    bottom: 0;
}

.body {
    width: 100%;
    background: blue;
    overflow: auto;
}

但是,当我滚动时,整个页面都会滚动,使我的页眉和页脚移动。 我可以使用 JavaScript 来解决这个问题,获取屏幕的高度并减去页眉和页脚的高度。

但是有什么方法我们可以只使用 css 来实现这一点吗?如果是的话怎么办?

最佳答案

很简单,像这样修改即可

.body {
    width: 100%;
    background: blue;
    overflow: auto;
    position: absolute;
    bottom: 40px;
    top: 40px;
}

检查这个http://jsfiddle.net/rCV6E/1/

关于javascript - 如何在不使用javascript的情况下使内容div可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18812174/

相关文章:

javascript - IE7-8 中的生涩视差

javascript - 从 html 文件访问导出的函数

javascript - 为 AngularJS + Spring MVC 网站生成静态 SEO 页面

javascript - 使用所需的键过滤 JSON

javascript - 如何将原始十六进制图像转换为 html img

css - 如何在 XPages 中定位 ExtLib 对话框而不使父级或屏幕居中

javascript - 选择标记 onchange 事件问题

javascript - js 如何使用自己的新 id 更改克隆表单内的 id

html - 如何创建一个表格,其中包含两个具有不同不透明度的内部嵌入式 imgclass 元素?

html - 带有 float 元素的 div 内的垂直对齐问题