2010-07-20 108 views
18

根据我在哪个模式的IE8(怪癖或标准),我得到不同的高度和宽度值。我试过标准的JavaScript和jQuery,但都返回不同的结果。获取网页的身体或窗口的高度和宽度

怪癖

$('body').width = 1239 
$('body').height = 184 
document.body.clientWidth = 1231 
document.body.clientHeight = 176 

在标准

$('body').width = 1260 
$('body').height = 182 
document.body.clientWidth = 1254 
document.body.clientHeight = 176 

任何想法如何获得通过IE8的模式不变的值。

谢谢你。

回答

10

也许问题所致。我没有IE(在Mac上),所以无法验证。

但是,我可以告诉你什么在我的项目jQuery Lightbox工作我没有这样的问题。我们使用下面的代码:

// Make the overlay the size of the body 
var $body = $(this.ie6 ? document.body : document); // using document in ie6 causes a crash 
$('#lightbox-overlay').css({ 
width: $body.width(), 
height: $body.height() 
}); 

// ... some code ... 

// Get the window dimensions 
var $window = $(window); 
var wWidth = $window.width(); 
var wHeight = $window.height(); 

并且叠加显示正确。我相信jQuery的宽度和高度结果与原生结果相比,因为jQuery自然应该考虑浏览器的任何怪癖。

重要的是要注意,由于某种原因,上面的灯箱脚本比$(document.body)更喜欢$(document)--我不记得对不起:O--也许这也解决了这个问题?

7

只是一个quickshot。试着让你的身体#page:以被包括在宽度和高度的滚动条,无论他们是否是有

function getPageHeight() { 
    var pageHeight = document.getElementById('page').offsetHeight; 
    var pageWidth = document.getElementById('page').offsetWidth; 

    alert(pageHeight); 
    alert(pageWidth); 
} 
+0

仍然没有运气。有些东西也很奇怪。 在标准高度= 200左右,怪异约为800. – 2010-07-20 09:36:27

+0

您能否解释为什么您需要区分ie8中的怪癖和标准模式? – gearsdigital 2010-07-20 09:46:43

+0

我不想但我需要页面在两种模式下显示相同。 – 2010-07-20 10:19:07

2

试试这个:

var viewport = { 
    width : $(window).width(), 
    height : $(window).height() 
}; 

var width = viewport.width; 
var height = viewport.height;