如何在浏览器打开时获取可用空间的当前高度和宽度。
我不想要整个文档的高度,只想要屏幕上可见的高度。
发布于 2009-12-01 02:53:16
您可以查看此blog post以了解该方法。
简而言之,就是给出代码
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)都提供了这样做的函数(IMHO)。
在jQuery中:
$(window).width();
$(window).height();
发布于 2013-09-01 05:06:02
使用窗口对象属性,内部高度/宽度,查找网页上内容的尺寸。这包括滚动条。这是-> http://goo.gl/L0cBLA的可视化解释。
高度:
window.innerHeight
宽度:
window.innerWidth
发布于 2010-11-29 17:50:06
<html id="THE_HTML">
<!-- all your stuff... -->
</html>
然后:
document.getElementById('THE_HTML').clientHeight;
在IE8和FF3.6下测试和工作...
https://stackoverflow.com/questions/1823691
复制