Javascript 获取窗口高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3012668/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:55:12  来源:igfitidea点击:

Get the window height

javascript

提问by Yo Momma

This is frustrating me. It should be something really simple but I can't get it to work in IE. I want to get the height of the current window: Not the scroll height, not the document height, but the actual window height. I've tried window.innerHeightwhich returns undefinedand document.documentElement.clientHeightwhich gives the scroll height.

这让我很沮丧。它应该非常简单,但我无法在 IE 中使用它。我想获取当前窗口的高度:不是滚动高度,不是文档高度,而​​是实际窗口高度。我试过window.innerHeight哪个返回undefineddocument.documentElement.clientHeight哪个给出滚动高度。

回答by Andy E

For current browsers

对于当前浏览器

window.innerHeight 

For IE 8 and lower, use

对于 IE 8 及更低版本,请使用

document.documentElement.offsetHeight;

If you need older browsers, use:

如果您需要旧版浏览器,请使用:

var height = "innerHeight" in window 
               ? window.innerHeight
               : document.documentElement.offsetHeight; 

回答by Alex Vang

I'm using:

我正在使用:

doc = document;
var theHeight = Math.max(
    doc.body.scrollHeight, doc.documentElement.scrollHeight,
    doc.body.offsetHeight, doc.documentElement.offsetHeight,
    doc.body.clientHeight, doc.documentElement.clientHeight
);

Found it here: Get document height (cross-browser) - James Padolsey

在这里找到: 获取文档高度(跨浏览器)- James Padolsey

And also found that jQueryis doing the same thing:

并且还发现jQuery也在做同样的事情:

// Either scroll[Width/Height] or offset[Width/Height] or client[Width/Height], whichever is greatest
// unfortunately, this causes bug #3838 in IE6/8 only, but there is currently no good, small way to fix it.
return Math.max(
  elem.body[ "scroll" + name ], doc[ "scroll" + name ],
  elem.body[ "offset" + name ], doc[ "offset" + name ],
  doc[ "client" + name ]
);

回答by pakore

http://www.javascripter.net/faq/browserw.htm

http://www.javascripter.net/faq/browserw.htm

Note that the code that uses document.body.offsetWidthand document.body.offsetHeightmust be executed after the browser has parsed the tag.

请注意,使用document.body.offsetWidth并且document.body.offsetHeight必须在浏览器解析标签后执行的代码。

Update: Try this

更新:试试这个

<script type="text/javascript">
<!--

 var viewportwidth;
 var viewportheight;

 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

 if (typeof window.innerWidth != 'undefined')
 {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
 }

// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
 }

 // older versions of IE

 else
 {
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
 }
document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
//-->
</script>

Found here

这里找到