RageZ..
21
您可以查看此博客文章以查看该方法.
简而言之就是给出那个代码
function alertSize() {
var myWidth = 0, myHeight = 0;
if(typeof(window.innerWidth) == 'number') {
// Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
}
else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
// IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
}
else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
// IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
window.alert( 'Width = ' + myWidth );
window.alert( 'Height = ' + myHeight );
}
还需要注意的是,大多数js框架(jquery,ext,prototype)都会提供这样做的功能(恕我直言).
在jQuery中:
$(window).width();
$(window).height();
-1 jQuery不返回可见区域的高度和宽度,它返回用户可以滚动到的高度和宽度. (7认同)
我发现了问题和解决方案:要有`$(window).width();`返回可见区域(而不仅仅是`$(document).width();`),确保有在HTML页面的最顶部包含``(或其他正确的`DOCTYPE`). (5认同)