Internet Explorer 内部高度

10
如何在Internet Explorer中获取窗口的innerHeight?谢谢。

5个回答

8
window.getWinSize= function(){
    if(window.innerWidth!= undefined){
        return [window.innerWidth, window.innerHeight];
    }
    else{
        var B= document.body, 
        D= document.documentElement;
        return [Math.max(D.clientWidth, B.clientWidth),
        Math.max(D.clientHeight, B.clientHeight)];
    }
}

4
在IE7和IE8中,应该使用Math.min()而不是Math.max(),至少这似乎是你想要的行为。 - Jonas Äppelgran

4

一个简单的一行解决方案:

var WinHeight = window.innerHeight || Math.max(document.documentElement.clientHeight, document.body.clientHeight);

1

这在IE9中有效:

document.body.clientHeight

我正在寻找在IE7和IE8中的方法。 - Shane Larson
抱歉,我的意思是它应该在IE6+中运行。我只在IE9中测试过它。kennebec的解决方案肯定更全面,尽管我个人之前使用过document.documentElement。 - Dan

0
在ie9中,只有当内容高度超过文档窗口时,window.innerHeightdocument.body.clientHeight才能正常工作。
一个可靠的解决方案是使用vwvh css属性。
css(简单)方法:
/* foce the content to be at 100% with css */
html, body {
    width: 100vw;
    height: 100vh;
}

JS之道:

// make a fitted htmlelement and returns its size.
var get_ie_size=function(){
    var domtest = document.createElement('div');
    domtest.style.display="block";
    domtest.style.width="100vw";
    domtest.style.height="100vh";
    document.body.appendChild(domtest);
    var size = [domtest.offsetWidth,domtest.offsetHeight];
    document.body.removeChild(domtest);
    return size;
};

-1
最简单的方法是使用jQuery。这里有一些详细信息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接