HTML DOM宽度+;可见窗高度

HTML DOM宽度+;可见窗高度,html,dom,height,width,Html,Dom,Height,Width,如何在浏览器打开时获取可用空间的当前高度和宽度 我不需要整个文档的高度,只需要屏幕上可以看到的高度。您可以查看此方法 简言之,给出代码 function alertSize() { var myWidth = 0, myHeight = 0; if(typeof(window.innerWidth) == 'number') { // Non-IE myWidth = window.innerWidth; myHeight = wi

如何在浏览器打开时获取可用空间的当前高度和宽度


我不需要整个文档的高度,只需要屏幕上可以看到的高度。

您可以查看此方法

简言之,给出代码

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();

在IE8和FF3.6下测试和工作…

使用窗口对象属性,内部高度/宽度,仅查找网页上内容的尺寸。这包括滚动条。这是一个直观的解释->

高度:

window.innerHeight
宽度:

window.innerWidth

-1 jQuery不返回可见区域的高度和宽度,它返回用户可以滚动到的高度和宽度,一些jQuery用户获得完整的可滚动大小,而一些jQuery用户获得当前可见大小。我发现了问题和解决方案:使用
$(window.width()
返回可见区域(而不仅仅是与
$(document).width()相同的值;
),确保HTML页面顶部包含
(或另一个适当的
DOCTYPE
)。
window.innerHeight
window.innerWidth