2012-06-18 58 views
5

我想要得到瀏覽器窗口正文的確切高度。我嘗試了innerHeight,clientHeight所有其他解決方案,這些解決方案是我在Google上搜索時獲得的。但沒有一個給出確切的高度。我必須通過從這些函數提供的高度減去一些值來調整高度。如何擺脫這個問題。我想我錯過了一些東西。請幫忙。如何獲得瀏覽器窗口正文的高度?

感謝

+0

jQuery解決方案能爲你工作嗎? – Dipak

回答

13

某些瀏覽器不正確地報告窗口高度 不同 - 尤其是移動的兄弟wsers,它們具有不同的視口概念。我有時使用函數來檢查幾個不同的值,返回最大的值。例如

function documentHeight() { 
    return Math.max(
     window.innerHeight, 
     document.body.offsetHeight, 
     document.documentElement.clientHeight 
    ); 
} 

編輯:我只是看着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 
    ); 
} 
+0

加1容易和*「非jQuery jQuery解決方案」*,ha – LinusGeffarth

+1

:)謝謝。現在有點老了,但可能仍然有效/有用?現在不要對瀏覽器/屏幕像素尺寸產生很大的影響 - 所有流體,彈性和「響應」:|比JavaScript/jQuery更多的CSS3和媒體查詢。 –

2

您可以使用jQuery實現這個...

$(document).ready(function(){ 
    browserWindowheight = $(window).height(); 
    alert(browserWindowheight); 
}); 
+1

他從來沒有說過他在使用jQuery。 – nunespascal

+2

我提到他必須使用jquery ...那麼爲什麼downvoting ..? – Subhajit

+2

他在哪裏說他不想使用jQuery? –

1

您是否嘗試過使用:

window.outerHeight (this is for IE9 and other modern browser's height) 

對於Internet Explorer與向後兼容模式,使用

document.body.offsetHeight 

並且還時,Internet Explorer(標準模式,document.compatMode == 'CSS1Compat'):

document.documentElement.offsetHeight 

看一看獲取更多信息如下:

相關問題