代码之家  ›  专栏  ›  技术社区  ›  Giffyguy

使用javascript获取浏览器窗口宽度/高度的最可靠方法是什么?

  •  1
  • Giffyguy  · 技术社区  · 13 年前

    我目前正在使用以下条件,但这些条件根本无法跨浏览器工作:

    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
    }
    

    我相信我遗漏了一两个条件——也许还有一个我没有考虑到的常见表达。

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

    2 回复  |  直到 11 年前
        1
  •  0
  •   gblazex    11 年前

    getWindowHeight()

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

    Tested


    How do I find the size of the window?

        2
  •  1
  •   Halil Özgür    13 年前

    $(window).width() $(window).height()