代码之家  ›  专栏  ›  技术社区  ›  Allain Lalonde

确定浏览器窗口在javascript中的位置?

  •  34
  • Allain Lalonde  · 技术社区  · 16 年前

    出于各种愚蠢的原因,我希望能够检测到屏幕上浏览器窗口的矩形。标题栏和所有。

    这是可能的,还是Javascript仅限于其页面的视图端口?

    编辑: 我可能不清楚,但视图端口是在窗口中可见的页面部分。这可能不是浏览器常用的术语,但在图形中很常见。

    3 回复  |  直到 9 年前
        1
  •  44
  •   John    9 年前

    对于符合标准的浏览器:

    X - window.screenX
    Y - window.screenY

    IE:

    X - window.screenLeft
    Y - window.screenTop

    记住, implementations vary . 注意多监视器设置…

        2
  •  9
  •   Shog9    16 年前

    查看以下属性:

    (这些不会提供窗口大小或位置,但允许您在具有多个监视器的系统上操作时正确解释这些窗口大小或位置)

        3
  •  -6
  •   Saiyine    16 年前

    复制从谷歌粘贴的第一个结果:

    // Browser Window Size and Position
    // copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
    // you may copy these functions but please keep the copyright notice as well
    function pageWidth() 
    {
    return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?  document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
    } 
    function pageHeight() 
    {
    return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
    } 
    function posLeft() 
    {
    return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
    } 
    function posTop() 
    {
    return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
    } 
    function posRight() 
    {
    return posLeft()+pageWidth();
    } 
    function posBottom() 
    {
    return posTop()+pageHeight();
    }