2008-10-05 87 views
1

我试图用javascript来设置元素的宽度和高度来覆盖整个浏览器视口,我成功地使用适当的IE6 HTML元素尺寸

document.body.clientHeight
,但在IE6中,似乎我总是会获得水平和垂直滚动条因为元素必须稍大。

现在,我真的不想使用浏览器特定的逻辑,并减去IE6中每个维度的像素或2。此外,我没有使用CSS(宽度:100%等),因为我需要像素数量。

有谁知道用IE6 +中的元素填充视口的更好方法(显然也是所有优秀的浏览器)?

编辑:感谢欧文的建议,我相信jQuery将工作。我应该指出我需要一个不受工具包影响的解决方案。

回答

3

你有没有考虑使用jQuery?它将大多数浏览器特定功能抽象为一个通用接口。

var width = $(document).width(); 
var height = $(document.height(); 

$('#mySpecialElement').width(width).height(height); 
+0

我希望我可以使用jQuery,但我没有控制: – 2008-10-13 14:51:16

3

可以利于事业......

http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/

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

嗯...不幸的是,这似乎并没有给我任何不同的计算从我一直在使用。感谢您的帮助,虽然:) – 2008-10-10 16:21:19

1

啊哈!我忘了

document.documentElement.clientLeft
document.documentElement.clientTop

他们在IE中是2,在好的浏览器中是0。 所以使用

var WIDTH = ((document.documentElement.clientWidth - document.documentElement.clientLeft) || document.body.clientWidth);
等做了伎俩,高度相似的想法!