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

确定浏览器窗口在JavaScript中的位置?,javascript,Javascript,出于各种愚蠢的原因,我希望能够检测屏幕上浏览器窗口的矩形。标题栏和所有 这是可能的,还是JavaScript仅限于其页面的查看端口 编辑:我可能不清楚,但视图端口是页面在窗口中可见的部分。这可能不是浏览器常用的术语,但在图形中很常见 从google第一个结果粘贴的副本: // Browser Window Size and Position // copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005 // you may copy these

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

这是可能的,还是JavaScript仅限于其页面的查看端口


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

从google第一个结果粘贴的副本:

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

对于符合标准的浏览器:

X-
Y-

对于IE:

X-
Y-


记住。注意多监视器设置…

查看以下属性:

  • :返回屏幕的高度(以像素为单位)
  • :返回距当前屏幕顶部的距离(以像素为单位)
  • :返回屏幕的宽度
  • :返回从主屏幕左侧到当前屏幕左侧的距离(以像素为单位)

(这些信息不会给出窗口大小或位置,但可以让您在具有多个监视器的系统上操作时正确解释它们)

来自同一页面:“要确定浏览器窗口中的可用空间以及该窗口在当前网页中的当前位置,您需要复制以下脚本并将其附加到页面的标题部分。”。“我想这意味着视口。仔细检查,这并不是我想要的。当我的窗口在屏幕中间时,警报(POSLIFT())返回0.pAgExpOffice返回“返回文档已经滚动水平的像素数。”-不是屏幕上的偏移,这不做海报所要求的。上一个谷歌排名靠前的搜索结果是旧的、丑陋的、糟糕的代码。现在,这个问题是谷歌最热门的结果,它有一个正确的答案,我今天刚刚试过,screenLeft和screenTop在IE 8中工作,但是screenX和screenY不再工作。看来我终于决定在这件事上合作了。还有其他人有同样的结果吗?这不是一个好答案。多监视器设置如何。这不起作用。请定义“工作”@Marco。没有多个监视器的意识,但它应该在返回坐标方面正常工作,这些坐标可用于在映射到监视器的虚拟“屏幕坐标系”内定位窗口。只是不要试图变得“聪明”——信息足以用于定位,但使用它来假设用户可以看到或想要看到什么是危险的。这些并不能替代适当的度量单位和响应性设计。MDN:“注意:
screenLeft
是旧属性的别名。
screenLeft
最初仅在IE中受支持,但由于受欢迎而被到处引入。”Chrome确实有window.screenTop和window.screenLeft,它们似乎是等效的。