2014-02-11 54 views
1

我正在創建一些javascript,需要確定輸入框上方和下方的可用空間量,並使用該值進行一些計算/操作。基本上我得到$ input.offset()。height來獲取上面的數量,然後從窗口的高度和輸入框的高度減去它以獲得下面的數量。javascript html height and window height

問題是我需要支持IE Quirks模式出於各種原因無法避免。窗口高度總是爲零或0,但如果我查詢html高度,它似乎給了我正確的窗口/視口高度。

代碼示例

$(window).height(); // This is 0 
$("html").height(); // This is fairly accurate for the viewport height 


var bottom_distance = $(window).height() - ($input.offset().top + $input.height); 
var quirks_distance = $("html").height() - ($input.offset().top + $input.height); // Is this reliable? 

我的問題是,我可以使用這個值$("html").height()可靠?

+3

如果你想整頁高度使用的$(document).height() – Jain

+0

任何機會描述你的主要目標是什麼?這聽起來並不是一個用這種方式呈現Web UI的好解決方案。它可以在多個瀏覽器上工作,具有很大的挑戰性,更不用說讓它在不同的移動設備上工作 – Ming

+0

我們的應用程序僅在IE上受支持。基本上我想要一個下拉菜單打開或關閉,具體取決於任何一邊有多少空間可用。我需要查詢下面的空間量,並將下拉的高度設置爲該空間內的值(除非它太小,然後再打開) – Ishikawa91

回答

1

給這個一展身手隊友:

編輯

<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> 

得到了這個網站的代碼: http://andylangton.co.uk/blog/development/get-viewport-size-width-and-height-javascript

我在4個版本的IE(6,8試過, 10,11)和3個不同的屏幕似乎可靠

我希望它有幫助。

但在IE 6這樣做的,下面好像在後面的痛苦,你希望與什麼工作上面IE 6

+1

只是一個鏈接的答案不是很有用。如果這被接受,然後有人發現這個問題,現在鏈接已經死了? –

+0

這正是我正在尋找的澄清。感謝您的詳細解答。幸好我不必擔心 Ishikawa91

1

試試這個: $(document).height();

+0

這會給我全高,我只需要「視口」的高度。 – Ishikawa91

相關問題