给大家分享一个让页脚始终保持在文档底部的代码,不喜勿喷。
HTML部分
<body>
<div style="height: 500px;width: 300px;background-color: blue;"></div>
<footer id="footer">
123456789
</footer>
</body>
CSS部分
* {
margin: 0px;
padding: 0px;
}
#footer {
width: 100%;
height: 30px;
background: red;
bottom: 0px;
text-align: center;
color: white;
}
JS部分
<script>
function a() {
return document.compatMode == "BackCompat" ? document.body.clientHeight : document.documentElement.clientHeight;
//标准兼容模式关闭,重新获取浏览器高度
}
var f = document.getElementById('footer');
(window.onresize = function() {
f.style.position = document.body.scrollHeight > a() ? '' : 'absolute';
})();
</script>
我是一位小白,如果大家有更好的方法可以留言评论,谢谢大家!