获取网页的正文或窗口的高度和宽度

Ash*_*nko 18 html javascript jquery

根据我所处的IE8模式(怪癖或标准),我得到不同的高度和宽度值.我已经尝试过标准的javascript和jquery,但两者都会返回不同的结果.

在怪癖中

$('body').width = 1239  
$('body').height = 184  
document.body.clientWidth = 1231  
document.body.clientHeight = 176  
Run Code Online (Sandbox Code Playgroud)

在标准中

$('body').width = 1260  
$('body').height = 182  
document.body.clientWidth = 1254  
document.body.clientHeight = 176
Run Code Online (Sandbox Code Playgroud)

任何想法如何通过IE8的模式获得值.

谢谢你.

bal*_*ton 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();
Run Code Online (Sandbox Code Playgroud)

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

需要注意的是上面的灯箱脚本更倾向于使用是很重要的$(document)$(document.body)出于某种原因-我不记得遗憾:O型-也许这可以解决这个问题呢?


gea*_*tal 7

只是一个快照.试着给你的身体#page:

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

    alert(pageHeight);
    alert(pageWidth);
}
Run Code Online (Sandbox Code Playgroud)