gpt4 book ai didi

JavaScript - 获取浏览器高度

转载 作者:IT王子 更新时间:2023-10-29 02:59:55 26 4
gpt4 key购买 nike

我正在寻找一个代码片段来获取浏览器窗口内可视区域的高度。

我有这段代码,但是它有点问题,好像主体没有超过窗口的高度然后又变短了。

document.body.clientHeight;

我尝试了其他一些方法,但它们要么返回 NaN,要么返回与上面相同的高度。

有谁知道如何获取浏览窗口的真实高度?

最佳答案

你会想要这样的东西,取自 http://www.howtocreate.co.uk/tutorials/javascript/browserwindow

function alertSize() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
window.alert( 'Width = ' + myWidth );
window.alert( 'Height = ' + myHeight );
}

所以这是现代浏览器的 innerHeight,IE 的 documentElement.clientHeight,弃用/怪癖的 body.clientHeight

关于JavaScript - 获取浏览器高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3333329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com