获取网页正文或窗口的高度和宽度 [英] Getting height and width of body or window of web page

查看:79
本文介绍了获取网页正文或窗口的高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我在哪个模式的IE8(怪癖或标准),我得到不同的高度和宽度值。我试过标准的javascript和jquery,但都返回不同的结果。

Depending on which mode of IE8 i'm in (quirks or standard) i get different values for the height and width. I've tried standard javascript and jquery but both return different results.

在怪癖中

$('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的模式获得不变的值。

Any ideas how to get a value unchanged by the mode of IE8.

非常感谢。

Thanks in adv.

推荐答案

也许这个问题是由于滚动条被包含在宽度和高度,不管他们是否在那里。我没有IE(在Mac上),所以无法验证。

Perhaps the issue is due to the scrollbars being included in the width and height regardless of whether or not they are there. I don't have IE (on a mac) so can't verify.

然而,我可以告诉你什么在我的项目中起作用 jQuery Lightbox 我没有这个问题。我们使用下面的代码:

However, I can tell you what does work as in my project jQuery Lightbox I have no such issue. We use the following code in it:

// 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自然应该考虑浏览器的任何怪癖。

And the overlay displays correctly. I would trust jQuery's result of the width and height compared to that of the native result, as jQuery should naturally be taking into account any quirks with the browser.

这很重要需要注意的是,上面的lightbox脚本往往比某些原因更喜欢 $(document) over $(document.body) - 我不记得对不起:哦 - 或许这也解决了这个问题?

It is important to note that the lightbox script above tends to prefer $(document) over $(document.body) for some reason - I can't remember sorry :O - so perhaps this solves the issue as well?

这篇关于获取网页正文或窗口的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆