javascript 如何获得网络浏览器窗口主体的准确高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11077475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get exact height of body of the webbrowser window?
提问by newprogress
I want to get exact height of the body of webbrowser window. I tried innerHeight, clientHeight all other solutions which I get while googling. but none of them give exact height. I had to adjust the height by subtracting some value from the height provided by these functions?? How can get out of this problem. I think I'm missing some thing. please help.
我想获得 webbrowser 窗口主体的确切高度。我在谷歌搜索时尝试了innerHeight、clientHeight所有其他解决方案。但他们都没有给出确切的高度。我必须通过从这些函数提供的高度中减去一些值来调整高度?怎样才能摆脱这个问题。我想我错过了一些东西。请帮忙。
Thanks
谢谢
回答by Ola Tuvesson
Some browsers report the window height incorrectlydifferently - particularly mobile browsers, which have a different viewport concept. I sometimes use a function to check several different values, returning whichever is the greatest. For example
某些浏览器以不同方式错误地报告窗口高度- 特别是移动浏览器,它们具有不同的视口概念。我有时会使用一个函数来检查几个不同的值,然后返回最大的值。例如
function documentHeight() {
return Math.max(
window.innerHeight,
document.body.offsetHeight,
document.documentElement.clientHeight
);
}
Edit:I just looked at how jQuery does it and it does indeed use Math.max and a series of properties - however the list it checks is slightly different to those in my example above, and since I usually trust the jQuery team to be better at this stuff than I am; here is the non-jQuery jQuery solution (if that makes any sense):
编辑:我只是看看 jQuery 是如何做到的,它确实使用了 Math.max 和一系列属性 - 但是它检查的列表与上面示例中的列表略有不同,因为我通常相信 jQuery 团队会更好在这件事上比我多;这是非 jQuery jQuery 解决方案(如果有任何意义):
function documentHeight() {
return Math.max(
document.documentElement.clientHeight,
document.body.scrollHeight,
document.documentElement.scrollHeight,
document.body.offsetHeight,
document.documentElement.offsetHeight
);
}
回答by Subhajit
You can use Jquery to achieve this...
您可以使用 Jquery 来实现此目的...
$(document).ready(function(){
browserWindowheight = $(window).height();
alert(browserWindowheight);
});
回答by skub
Have you tried using:
您是否尝试过使用:
window.outerHeight (this is for IE9 and other modern browser's height)
For Internet Explorer with backward-compatibility mode, use
对于具有向后兼容模式的 Internet Explorer,请使用
document.body.offsetHeight
And also, Internet Explorer (standards mode, document.compatMode=='CSS1Compat'):
还有,Internet Explorer(标准模式,document.compatMode=='CSS1Compat'):
document.documentElement.offsetHeight
Have a look at the following for more information:
请查看以下内容以获取更多信息: