我正在尝试让脚本正常工作,但有点困难。
我需要创建一个在页面加载时显示的 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>
如有任何帮助,我们将不胜感激。 :-)
最佳答案
你想要 docheight = window.innerHeight || document.documentElement.clientHeight;
编辑:尽管请记住人们可以调整他们的窗口大小,所以除非您监视 window.onresize
,否则要小心。
关于javascript - 如何使用javascript设置一个div来匹配浏览器窗口的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8575675/