这令我很沮丧。它应该是非常简单的东西,但我不能让它在IE中工作。我想获得当前窗口的高度:不是滚动高度,不是文档高度,而是实际的窗口高度。我试过window.innerHeight
,它返回undefined
和document.documentElement.clientHeight
,它给出了滚动高度。获取窗口高度
获取窗口高度
回答
对于IE 8和更低,使用
document.documentElement.offsetHeight;
所以跨浏览器应该是:
var height = "innerHeight" in window
? window.innerHeight
: document.documentElement.offsetHeight;
http://www.javascripter.net/faq/browserw.htm
注意,在浏览器解析标签后,使用
document.body.offsetWidth
和
document.body.offsetHeight
代码必须执行。
更新: 试试这个
<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>
找到here
不,不工作,给人的滚动高度 – 2010-06-10 08:16:52
@YO Momma:你可以使用下面的代码:myWidth = screen.availWidth; myHeight = screen.availHeight;'这适用于所有浏览器。一件事是那个window.innerheight不支持在IE – Raje 2011-08-25 09:05:20
我使用:
doc = document;
var theHeight = Math.max(
doc.body.scrollHeight, doc.documentElement.scrollHeight,
doc.body.offsetHeight, doc.documentElement.offsetHeight,
doc.body.clientHeight, doc.documentElement.clientHeight
);
在这里找到它: Get document height (cross-browser) - 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 ]
);
误导。由于'scrollHeight'给出[元素内容的高度,包括屏幕上不可见的内容](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight),代码给出* *文件高度**。但问题是获得**窗口高度**。 – Lightman 2016-07-23 07:48:32
- 1. 实时获取窗口高度/宽度
- 2. 获取X11窗口标题高度
- 3. 通过jQuery获取窗口高度
- 4. 钛 - 获取当前窗口的高度
- 5. Linux:获取窗口边框高度
- 6. 使用jquery获取窗口高度
- 7. 获取窗口的高度(不滚动)
- 8. JavaScript窗口高度
- 9. CSS - 窗口高度
- 10. 如何获取活动的窗口宽度和高度?
- 11. 获取网页的身体或窗口的高度和宽度
- 12. SDL:获取窗口高度/宽度/矩形?
- 13. Gtk.window使用VALA获取窗口宽度和高度
- 14. 在C中获取活动窗口坐标和高度宽度#
- 15. jQuery Mobile弹出窗口内容高度超过窗口高度
- 16. 内容100%窗口高度的高度
- 17. 获取窗口高度并将div大小设置为此高度
- 18. 如何在android中获取未使用的窗口高度
- 19. javascript:获取OUTER(真实)浏览器窗口高度?
- 20. 如何从iFrame中使用jQuery获取父窗口的高度?
- 21. 如何从iframe获取Facebook窗口高度?
- 22. 获取ie8输入浏览器窗口的高度
- 23. 获取图像的高度以设置模态窗口大小
- 24. 如何从SurfaceView中获取窗口高度?
- 25. 在平板电脑上获取窗口高度问题
- 26. jQuery的窗口调整大小获取元素的高度
- 27. CSS获取浮动DIV以延伸至窗口高度
- 28. 获取有关窗口的div高度调整
- 29. 在jQuery AJAX之后获取新窗口高度
- 30. 窗口高度 - 140像素
安迪,Flippen唉男人,它的工作原理,谢谢 – 2010-06-10 08:35:48
我添加'window.innerHeight'是为IE9 + – vsync 2014-08-13 10:12:40
我成功地使用这种技术在基于页面的页面在IE 8/9/10和Firefox下也是如此。 Thankyou – 2014-08-26 07:21:08