如何获取浏览器打开时可用空间的当前高度和宽度。
我不需要整个文档的高度,只需要屏幕上可见的高度。
你可以看看这个博客文章 http://www.howtocreate.co.uk/tutorials/javascript/browserwindow查看方法。
简而言之,给出该代码
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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)