tags:

views:

35

answers:

3

Hi Guys,

If I have a flowing layout (position: static / relative), does the browser store the calculated coordinates (x,y) of a div in properties which can be accessed?

Further, it would suffice if the solution worked with Firefox only. JQuery is unfortunately, not an option.

+1  A: 

It does indeed. Unfortunately it's pretty difficult to get that information out reliably due to browser inconsistencies and general ugliness of raw DOM access.

I suggest jQuery, where you might have code like:

$('#some_div').offset().top

Which will give you the y position of the div from the top left of the document.

thenduks
Unfortunately, I am working on a legacy system and need a quick fix. The system does not include the JQuery files. The good thing is that I only need the system to work with Firefox.
Crimson
+1  A: 

No, however using mootools (and probably jquery) you can say $(element).getLeft() or $(element).getTop().

or you could use something like this:

function getLeft(obj) {
    return (obj.offsetParent==null ? obj.offsetLeft : obj.offsetLeft + getLeft(obj.offsetParent));
}

function getTop(obj) {
    return (obj.offsetParent==null ? obj.offsetTop : obj.offsetTop + getTop(obj.offsetParent));
}
Rob
+1  A: 

In 'native' JavaScript you can do it like that:

document.getElementById('yourElement').offsetLeft
document.getElementById('yourElement').offsetTop

but you'd probably need to add up few offsets of parent elements depends what position is applied.

rochal
Worked perfectly. Thanks :)
Crimson
Note that that is the coordinates relative to the parent container, which won't give you accurate results in all cases.
Rob
I knew the parent container's coordinates. This was exactly what was needed :)
Crimson