FrontEnd 步步高升:header和footer始终显示在浏览器顶部和底部,中间区域自适应

思路:

  1. 浏览器大小改变会触发JS事件:window.onresize
  2. 获得浏览器大小 减去 顶部+底部 得到中间区域大小


HTML:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link href="css/one.css" rel="stylesheet">
    <script src="js/one.js" type="text/javascript"></script>
</head>
<body>
    <header id="headBlock"></header>
    <div    id="bodyBlock" class="body"></div>
    <footer id="footBlock"></footer>
</body>
</html>


CSS:

/* 韦哥说 这里是必须的,这才是真正项目用到的 */
html{
    height: 100%:
}
body{
    height: 100%;
    margin: 0;
}
/**********************************************/
#headBlock {
    height: 60px;
    background-color: red;
}

#footBlock {
    height: 60px;
    background-color: yellow;
}

.body {
    background-color: blue;
    overflow-y: scroll;
}


JS:

function reSizeBodyBlock() {
    "use strict";
    var browserHeight = document.documentElement.clientHeight,
        headHeight    = document.getElementById("headBlock").offsetHeight,
        footHeight    = document.getElementById("footBlock").offsetHeight,
        bodyBlockHeight = (browserHeight - headHeight - footHeight).toString() + "px";  //为什么要加toString()? 因为代码要给更多的人看!!!!
    document.getElementById("bodyBlock").style.height = bodyBlockHeight;
}

window.onload = window.onresize = reSizeBodyBlock;



/* ------------------------------------------------------------------ 2015-02-12 17:03:43 ------------------------------------------------------------------ */

更简单灵活的方法:

function initFullBlockBlock() {
    if (!document.getElementById("full-block")) return;
    var fullBlockHeight = document.getElementById("full-block").offsetHeight,
        browserHeight = document.documentElement.clientHeight,
        bodyHeight = document.getElementsByTagName("body")[0].offsetHeight;
    bodyBlockHeight = (browserHeight - bodyHeight + fullBlockHeight).toString() + "px";
    document.getElementById("full-block").style.height = bodyBlockHeight;

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值