2010-06-10 66 views
29

Điều này làm tôi bực mình. Nó phải là một cái gì đó thực sự đơn giản nhưng tôi không thể làm cho nó hoạt động trong IE. Tôi muốn nhận được chiều cao của cửa sổ hiện tại: Không phải chiều cao cuộn, không phải chiều cao tài liệu, nhưng chiều cao cửa sổ thực tế. Tôi đã thử window.innerHeight trả về undefineddocument.documentElement.clientHeight cho độ cao cuộn.Lấy chiều cao cửa sổ

Trả lời

68

Đối với IE 8 trở xuống, sử dụng

document.documentElement.offsetHeight; 

trình duyệt Vì vậy chéo nên là:

var height = "innerHeight" in window 
       ? window.innerHeight 
       : document.documentElement.offsetHeight; 
+1

Andy, Flippen Ay man, nó hoạt động, nhờ –

+0

Tôi đang thêm 'window.innerHeight' cho IE9 + – vsync

+0

Tôi đang sử dụng thành công kỹ thuật này trong trang dựa trên khung dưới IE 8/9/10 và Firefox. Thankyou –

1

http://www.javascripter.net/faq/browserw.htm

Lưu ý rằng mã có sử dụng document.body.offsetWidthdocument.body.offsetHeight phải được thực hiện sau khi trình duyệt đã phân tách các thẻ.

Cập nhật: Hãy thử điều này

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

Tìm thấy here

+0

nope, doesnt làm việc, mang lại cho chiều cao cuộn –

+0

@YO Momma: bạn có thể sử dụng mã sau 'myWidth = screen.availWidth; myHeight = screen.availHeight; 'hoạt động này trong tất cả các trình duyệt. một điều là window.innerheight không hỗ trợ trong IE – Raje

5

Tôi đang sử dụng:

doc = document; 
var theHeight = Math.max(
    doc.body.scrollHeight, doc.documentElement.scrollHeight, 
    doc.body.offsetHeight, doc.documentElement.offsetHeight, 
    doc.body.clientHeight, doc.documentElement.clientHeight 
); 

Tìm thấy nó ở đây: Get document height (cross-browser) - James Padolsey

Và cũng thấy rằng jQuery đang làm điều tương tự:

// 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 ] 
); 
+0

gây hiểu lầm. Vì 'scrollHeight' cung cấp cho [chiều cao của nội dung của phần tử, bao gồm nội dung không hiển thị trên màn hình] (https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight), mã cung cấp * * chiều cao tài liệu **. Tuy nhiên, câu hỏi là để có được ** chiều cao cửa sổ **. – Lightman

Các vấn đề liên quan