Rag*_*geZ 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 );
}
Run Code Online (Sandbox Code Playgroud)
还需要注意的是,大多数js框架(jquery,ext,prototype)都会提供这样做的功能(恕我直言).
在jQuery中:
$(window).width();
$(window).height();
Run Code Online (Sandbox Code Playgroud)
使用窗口对象属性(内部高度/宽度)来查找网页上内容的尺寸。这包括滚动条。这是一个直观的解释 - > http://goo.gl/L0cBLA。
高度:
window.innerHeight
Run Code Online (Sandbox Code Playgroud)
宽度:
window.innerWidth
Run Code Online (Sandbox Code Playgroud)