How can I get an object's absolute position on the page in Javascript?

JavascriptDomOffset

Javascript Problem Overview


I want to get an object's absolute x,y position on the page in Javascript. How can I do this?

I tried obj.offsetTop and obj.offsetLeft, but those only give the position relative to the parent element. I guess I could loop through and add the parent's offset, and its parent's offset, and so on until I get to the object with no parent, but it seems like there should be a better way. Googling didn't turn up much, and even SO site search isn't finding anything.

Also, I can't use jQuery.

Javascript Solutions


Solution 1 - Javascript

var cumulativeOffset = function(element) {
    var top = 0, left = 0;
    do {
        top += element.offsetTop  || 0;
        left += element.offsetLeft || 0;
        element = element.offsetParent;
    } while(element);

    return {
        top: top,
        left: left
    };
};

(Method shamelessly stolen from PrototypeJS; code style, variable names and return value changed to protect the innocent)

Solution 2 - Javascript

I would definitely suggest using [element.getBoundingClientRect()][1].

https://developer.mozilla.org/en-US/docs/Web/API/element.getBoundingClientRect

> ## Summary ## > > Returns a text rectangle object that encloses a group of text > rectangles. > > ## Syntax ## > > var rectObject = object.getBoundingClientRect(); > > ## Returns ## > > The returned value is a [TextRectangle][2] object which is the union of the > rectangles returned by [getClientRects()][3] for the element, i.e., the CSS > border-boxes associated with the element. > > The returned value is a TextRectangle object, which contains read-only > left, top, right and bottom properties describing the border-box, in > pixels, with the top-left relative to the top-left of the viewport.

Here's a browser compatibility table taken from the linked MDN site:

+---------------+--------+-----------------+-------------------+-------+--------+
|    Feature    | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
+---------------+--------+-----------------+-------------------+-------+--------+
| Basic support | 1.0    | 3.0 (1.9)       | 4.0               | (Yes) | 4.0    |
+---------------+--------+-----------------+-------------------+-------+--------+

It's widely supported, and is really easy to use, not to mention that it's really fast. Here's a related article from John Resig: http://ejohn.org/blog/getboundingclientrect-is-awesome/

You can use it like this:

var logo = document.getElementById('hlogo');
var logoTextRectangle = logo.getBoundingClientRect();

console.log("logo's left pos.:", logoTextRectangle.left);
console.log("logo's right pos.:", logoTextRectangle.right);

Here's a really simple example: http://jsbin.com/awisom/2 (you can view and edit the code by clicking "Edit in JS Bin" in the upper right corner).

Or here's another one using Chrome's console: [![Using element.getBoundingClientRect() in Chrome][4]][5]

Note:

I have to mention that the width and height attributes of the getBoundingClientRect() method's return value are undefined in Internet Explorer 8. It works in Chrome 26.x, Firefox 20.x and Opera 12.x though. Workaround in IE8: for width, you could subtract the return value's right and left attributes, and for height, you could subtract bottom and top attributes ([like this][6]).

[1]: https://developer.mozilla.org/en-US/docs/Web/API/element.getBoundingClientRect "element.getBoundingClientRect" [2]: https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIDOMClientRect [3]: https://developer.mozilla.org/en-US/docs/Web/API/element.getClientRects?redirectlocale=en-US&redirectslug=DOM/element.getClientRects [4]: http://i.stack.imgur.com/eDkj3.png [5]: http://i.stack.imgur.com/eDkj3.png [6]: http://jsbin.com/awisom/3/edit

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKipView Question on Stackoverflow
Solution 1 - JavascripteyelidlessnessView Answer on Stackoverflow
Solution 2 - JavascriptSk8erPeterView Answer on Stackoverflow