简体   繁体   English

动态调整div高度以在内容未填满窗口的情况下将页脚保持在底部

[英]dynamically adjusting div height to keep footer at bottom if content doesn't fill window

I need to adjust the container div height so that it keeps the footer at the bottom of the window onload and onresize when the content is too short but not if content pushes the footer off. 我需要调整容器div的高度,以便在内容太短时将页脚保持在窗口底部,并在调整大小时保持onresize,但在内容将页脚推离的情况下则不需要。 I've tried variations of the css min-height:100% but it doesn't work. 我试过了CSS最小高度:100%的变体,但是没有用。 I've managed to make it work onload with this: 我已经设法使其与此一起工作:

<div class="header></div>
<script>
 var h = window.innerHeight-205;
 document.write('<div id="container" class="container" style="min-height: ' + h + 'px;">');
</script>
  ....content of container....
</div>
<div class="footer"></div>

but when the window is resized my footer ends up in the middle of the page or off the page. 但是当调整窗口大小时,我的页脚最终出现在页面中间或页面外。 I've tried calling a resize function but never seem to get it to reset the height. 我试过调用一个调整大小的函数,但似乎从未获得过它来重置高度。 The codes I tried were: 我尝试的代码是:

function resetHeight () {
var h = window.innerHeight-205;
document.getElementById("container").setAttribute("height",h);
}

and

function resetHeight () {
var h = window.innerHeight-205;
document.getElementById("container").height = h;
}

I'm trying to use only javascript and css, not jquery. 我正在尝试仅使用javascript和CSS,而不使用jquery。 I'm not too familiar with JS so if I'm missing something to make the function call work please let me know! 我对JS不太熟悉,所以如果我缺少使函数调用正常工作的功能,请告诉我! I'm not concerned about older browsers, just IE9+ and such, it also needs to work on iPads when the user rotates their screen... 我不关心较旧的浏览器,只有IE9 +等,当用户旋转屏幕时,它也需要在iPad上运行...

Any help will be appreciated! 任何帮助将不胜感激!

尝试使用CSS方法,例如此处概述的方法: http : //www.cssstickyfooter.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM