目前我正在尝试使用 Javascript 将页脚保持在底部。这是结果:
document.getElementsByTagName('body').onload = function() {KeepFoot()};
var element = document.getElementById('container');
var height = element.offsetHeight;
function KeepFoot() {
if (height < screen.height) {
document.getElementById("footer").style.position = "fixed";
document.getElementById("footer").style.bottom = "0";
document.getElementById("footer").style.left = "0";
document.getElementById("footer").style.right = "0";
}
}
我的想法是获取div容器的高度并将其与pc分辨率的高度进行比较。如果div容器的高度小于PC分辨率的高度,设置为div footer position: fixed;
但脚本中存在问题,因为它不起作用。
另一个问题,我创建的脚本是否适合将页脚保留在底部?
HTML:
<html>
<head>
...
</head>
<body>
<div id="container">
<div id="header"></div>
<div id="content"></div>
<div id="footer"></div>
</div>
</body>
</html>
最佳答案
该函数未在加载时调用。您可以像这样将函数 KeepFoot 直接附加到 body 标记,而不是像这样调用:
document.getElementsByTagName('body').onload = function() {KeepFoot()};
或者使用我下面的代码:
(function() {
var offsetHeight = document.getElementById('container').offsetHeight;
var screenHeight = screen.height;
if(offsetHeight < screenHeight){
document.getElementById("footer").style.position = "fixed";
document.getElementById("footer").style.bottom = "0";
document.getElementById("footer").style.left = "0";
}
})();
关于javascript - 使用 Javascript 将页脚保持在底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30276950/