這令我很沮喪。它應該是非常簡單的東西,但我不能讓它在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