FrontEnd 步步高升:header和footer始終顯示在瀏覽器頂部和底部,中間地區自適應,frontendfooter
思路:
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;