2

So based on this question I asked, what's the most reliable way of getting position of objects that's crossbrowser? Thx

Community
  • 1
  • 1
user1019031
  • 743
  • 7
  • 16
  • jQuery's `position()`? It works for DOM elements, the `document` object, and the `window` object, so you're covered. – Šime Vidas Jan 27 '12 at 22:22

3 Answers3

7

In general, assuming you have an element named elem, it's actually quite easy to get the X and Y coordinates of the top-left corners of an element, assuming you want these in document coordinates. In all browsers this is returned by the elem.offsetLeft and elem.offsetTop properties.

The only trick you have to be aware of is that if elem is absolutely positioned in another element, say a div with a left / top margin of 20px, these properties will return 0, as it only takes into account the current element and not the entire chain of elements. Luckily we can use a "chain-traversal" function to capture all of the margins of elements associated with a given element, tally them up to get the correct document coordinates.

As Sime Vidas mentioned, there is also JQuery's position() and offset() properties, in this case you would want the offset() properties.

You can also use the getBoundingClientRect() method, however this returns the coordinates of an element relative to its offsetParent and thus is not entirely reliable. Look at the following examples:

// getPosition function
function getPosition(elem){

    var dims = {offsetLeft:0, offsetTop:0};

    do {
        dims.offsetLeft += elem.offsetLeft;
        dims.offsetTop += elem.offsetTop;
    }

    while (elem = elem.offsetParent);

    return dims;
}

cont1.style.position = "absolute";
cont1.style.marginLeft = "10px";

cont2.style.position = "absolute";
cont2.style.marginLeft = "10px";

box.style.position = "absolute";
box.style.marginLeft = "10px";


console.log(getPosition(box).offsetLeft); // returns "30"
console.log(getPosition(box).offsetTop); // returns "0"

// or in JQuery
console.log($(box).offset().left) // also returns "30"
console.log($(box).offset().top) // also returns "0"

Also I suggest you read this.

dkugappi
  • 2,664
  • 5
  • 21
  • 22
0

If you want to find the position of an element relative to document use jQuery offset() method.

var p = $("p:last");
var offset = p.offset();
p.html( "left: " + offset.left + ", top: " + offset.top );

.offset() reference: http://api.jquery.com/offset/

If you want to find the poistion of an element relative to its parent then use jQuery position() method.

var p = $("p:first");
var position = p.position();
$("p:last").text( "left: " + position.left + ", top: " + position.top );

.position() reference: http://api.jquery.com/position/

And these methods almost gives perfect result in most of the browsers.

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
0

I like element.getBoundingClientRect(). It has good cross-browser support.

var coords = element.getBoundingClientRect();

This gives the coordinates relative to the viewport. To get the coordinates relative to the document, add document.documentElement.scrollTop to the top and document.documentElement.scrollLeft to the left.

coords.top += document.documentElement.scrollTop;
coords.left += document.documentElement.scrollLeft;

But since you are already using jQuery, you may as well just use .offset().

gilly3
  • 87,962
  • 25
  • 144
  • 176