屏幕宽度vs可见部分
问题描述:
我对JavaScript有点问题,为了获得我们使用screen.width的屏幕宽度,它返回整个屏幕分辨率,是否有一个命令来获得浏览器可见部分的分辨率,如当浏览器没有最大化?屏幕宽度vs可见部分
答
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()
。
答
你所描述的区域是视口,通常可以通过在现代浏览器中使用window.innerWidth或window.innerHeight访问。 IE6有点不同,但在this tutorial on obtaining viewport size中可以找到有关处理所有浏览器的视口宽度的更多信息。
答
我看到这个问题得到了回答,但这里是我用一个图像来说明的代码。
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());
});
谢谢主席先生,window.innerWidth和window.innerHeight是我想要的东西,它只是工作!但为什么所有其他的代码? – med 2011-02-13 22:30:49