2011-12-20 75 views
2

我试图让脚本工作,但我挣扎了一下。如何设置div来匹配浏览器窗口使用javascript的高度?

我需要创建一个在页面加载时显示的div,并且该div下面的任何内容都将从页面底部推出,并且您需要向下滚动才能查看它。

这里是我有什么,但它不工作(我包括警报,这样我可以检查脚本的第一部分是确定的工作)

<script type="text/javascript"> 
function getDocHeight() { 
var D = document; 
return Math.max(
    Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), 
    Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), 
    Math.max(D.body.clientHeight, D.documentElement.clientHeight) 
); 
} 
var docheight = (getDocHeight()) +'px'; 
// alert(docheight); 
document.getElementById('pusher').style.height = docheight; 
</script> 

<div id="pusher">This content is shown on the page</div> 
<p>Anything here is below the fold of the page and you will need to scroll down to see me</p> 

任何帮助将是非常感激地收到。 :-)

+1

你也许可以用纯CSS做到这一点。 – Matt 2011-12-20 12:52:36

+0

可能重复[获取窗口高度](http://stackoverflow.com/questions/3012668/get-the-window-height) – 2011-12-20 12:55:18

+0

是的,'html,body,#pusher {height:100%; }'? – 2011-12-20 12:57:04

回答

1

你想docheight = window.innerHeight || document.documentElement.clientHeight;

编辑:虽然,牢记人民可以调整自己的窗口,所以,除非你监视window.onresize,要小心。

相关问题