如何使用JavaScript找到HTML元素相对于整个桌面屏幕的位置?

| 如何使用JavaScript找到相对于桌面屏幕(即浏览器窗口之外)的特定html元素(例如div,表,标签等)的X,Y坐标? 我可以使用
offsetHeight
offsetWidth
来找到元素的高度和宽度,但是找不到任何可以为我提供元素相对于用户整个桌面屏幕的精确X,Y坐标的东西。     
已邀请:
我认为您必须跟随树,通过父母,并继续添加偏移量,如下所示: http://bytes.com/topic/javascript/answers/90547-how-get-absolute-position-element
function getY( oElement )
{
    var iReturnValue = 0;
    while( oElement != null ) {
        iReturnValue += oElement.offsetTop;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}
    
我认为即使很简单也不不可能。 查找浏览器相对于屏幕的位置 查找相对于视口的元素位置。 添加坐标,您将获得所需的输出 遵循我编写的代码:
var element = document.getElementById(\"ID of the element\");// you can use any method to find the element ..
var position = getPosition(element);
function getPositions(obj)
{
        var p = [];
        var position =  obj.getBoundingClientRect();
        p[0] = window.screenX + position.left;
        p[1] = window.screenY + position.top;
        p[2] = position.width;
        p[3] = position.height;
        return p;

}
    

要回复问题请先登录注册