获得窗口高度

Yo *_*mma 32 javascript

这令我很沮丧.它应该是非常简单的东西,但我不能让它在IE中工作.我想得到当前窗口的高度:不是滚动高度,不是文档高度,而​​是实际窗口高度.我试过window.innerHeight哪个返回undefined,document.documentElement.clientHeight它给出了滚动高度.

And*_*y E 78

对于当前浏览器

window.innerHeight 
Run Code Online (Sandbox Code Playgroud)

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

document.documentElement.offsetHeight;
Run Code Online (Sandbox Code Playgroud)

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

var height = "innerHeight" in window 
               ? window.innerHeight
               : document.documentElement.offsetHeight; 
Run Code Online (Sandbox Code Playgroud)


Ale*_*ang 5

我正在使用:

doc = document;
var theHeight = Math.max(
    doc.body.scrollHeight, doc.documentElement.scrollHeight,
    doc.body.offsetHeight, doc.documentElement.offsetHeight,
    doc.body.clientHeight, doc.documentElement.clientHeight
);
Run Code Online (Sandbox Code Playgroud)

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

并且还发现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 ]
);
Run Code Online (Sandbox Code Playgroud)

  • 误导。由于 `scrollHeight` 给出了[元素内容的高度,包括屏幕上不可见的内容](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight),代码给出 * *文件高度**。然而问题是获得**窗口高度**。 (3认同)