屏幕宽度与可见部分

时间:2011-02-13 22:21:31

标签: javascript screen

我对JavaScript有一点问题,要获得我们使用screen.width的屏幕宽度,它返回整体屏幕分辨率,是否有一个命令来获取浏览器可见部分的分辨率,就像浏览器不是最大化?

3 个答案:

答案 0 :(得分:28)

function width(){
   return window.innerWidth 
       || document.documentElement.clientWidth 
       || document.body.clientWidth 
       || 0;
}

function height(){
   return window.innerHeight 
       || document.documentElement.clientHeight 
       || document.body.clientHeight 
       || 0;
}

使用它们返回可见窗口的height()width()

JSFiddle example.

答案 1 :(得分:1)

您所描述的区域是视口,通常可以在现代浏览器中使用 window.innerWidth window.innerHeight 进行访问。 IE6略有不同,但可以在this tutorial on obtaining viewport size中找到有关处理所有浏览器视口宽度的更多信息。

答案 2 :(得分:1)

我看到问题已经回答了,但这里是我用图片说明的代码。

function height(){
return(window.innerHeight)?
window.innerHeight:
document.documentElement.clientHeight||document.body.clientHeight||0;
}
$(document).ready(function(){
$('.first, .second').css('height',height());
});
$(window).resize(function() {
$('.first, .second').css('height',height());
});

JSFiddle Example