1

我目前正在使用以下条件,但它们不能跨浏览器或根本不工作:

if (typeof (window.innerHeight) == 'number') {
    //Non-IE:
    //Perform operation using window.innerWidth/Height
} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    //IE 6+ in 'standards compliant mode'
    //Perform operation using document.documentElement.clientWidth/Height
} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
    //IE 4 compatible
    //Perform operation using document.body.clientWidth/Height
}

我相信我错过了一个或两个条件——也许还有另一个我没有考虑到的常见表达方式。

我需要添加什么才能完成?

4

2 回答 2

1

在您的页面中包含 jQuery 并$(window).width()使用 $(window).height().

于 2010-11-08T18:34:36.087 回答
0

你最好把整个逻辑包装成一个函数,所以你只需要说getWindowHeight()

// cross browser window height
function getWindowHeight() {
  return window.innerHeight || 
         (document.documentElement && document.documentElement.clientHeight) || 
         (document.body && document.body.clientHeight);
}

测试:IE5.5+、FF 2+、Chrome、Opera

但要获得更强大的方法,请访问相关的 comp.lang.javascript 常见问题解答条目:
如何找到窗口的大小?

于 2010-11-08T18:40:56.820 回答