好的,请考虑这张图片.
I develop an add-on firefox and I would :
- the distance in red, between top of screen and top of `visible webpage`
- the distance in red between left of screen and left of `visible webpage`
- the distance in green
有什么意义?
我有一千个屏幕坐标(X,Y),我必须相对于网页计算坐标.
Example :
Considering
Screen size : 1200 * 800
Webpage size : 400*300
Red distance between left screen border and left webpage border : 200
Red distance between top screen border and top webpage border : 300
So my coordinates screen => relative webpage becomes :
( 100, 100 ) => OUTSIDE WEBPAGE( ignored )
( 1100, 650 ) => OUTSIDE WEBPAGE ( ignored )
( 200, 300 ) => ( 0,0 )
( 250, 400 ) => ( 50, 100 )
我已经有了页面的宽度和高度
$(window).width()
$(window).heigth()
因此,如果我可以获得选项卡的左角坐标,我可以确定屏幕上选项卡的机器人角落.
我已经在这里提出了这个问题,但对于IE扩展.如果您需要更多细节.但我无法使用firefox的答案.
Get distance between screen edge and webpage
最佳答案 DOMWindows有mozInnerScreenX,pageXOffset和screenX
我用软件将屏幕移动到0,0我得到了这个:
aDOMWindow.mozInnerScreenX:8
DOMWindow.screenX:0
如果我最大化窗口
aDOMWindow.mozInnerScreenX:-6
DOMWindow.screenX:0
所以让我们使用screenX.
现在,如果您有一个gBrowser,那就是您在上面指出的“内部窗口”,我们看到boxObject有一个screenX.
所以它看起来只是做:
var aDOMWindow = Services.wm.getMostRecentWindow('navigator:browser'/*null*/);
console.log(aDOMWindow.mozInnerScreenX, aDOMWindow.screenX)
if (aDOMWindow.gBrowser) {
var innerWin = aDOMWindow.gBrowser.boxObject;
console.log(innerWin.screenX, innerWin.screenY);
}