Getting x and y cordinates of a div

S

Seth Illgard

Hi. I need to obtain the exact postion of a div. I was using
(variable and function names are in spanish, sorry):

//Get X position
function obtenerPosicionX(elemento)
{
var x=0;
while(elemento)
{
x += elemento.offsetLeft;
elemento=elemento.offsetParent;
}
return x;
}

//Get Y position
function obtenerPosicionY(elemento)
{
var y=0;
while(elemento)
{
y += elemento.offsetTop;
elemento=elemento.offsetParent;
}
return y;
}

It works great!, except when the div (elemento in my script) is inside
an absolute positioned div. I need to obtain the coordintes no matter
where in the DOM tree the div is.

Any ideas? Thanks
 
R

RobG

Hi. I need to obtain the exact postion of a div. I was using
(variable and function names are in spanish, sorry):

//Get X position
function obtenerPosicionX(elemento)
{
var x=0;
while(elemento)
{
x += elemento.offsetLeft;
elemento=elemento.offsetParent;
}
return x;

}

//Get Y position
function obtenerPosicionY(elemento)
{
var y=0;
while(elemento)
{
y += elemento.offsetTop;
elemento=elemento.offsetParent;
}
return y;

}

It works great!, except when the div (elemento in my script) is inside
an absolute positioned div. I need to obtain the coordintes no matter
where in the DOM tree the div is.

Any ideas? Thanks

Quirksmode has an example with an explanation of how it works:

<URL: http://www.quirksmode.org/js/findpos.html >

The offsetParent strategy is not always correct. Matt Kruse has a
position library here with commented code:

<URL: http://www.javascripttoolbox.com/lib/objectposition/ >
 
S

Seth Illgard

exact, you said?
x and y coordinates are always relative to a reference coordinates
system. So, what you is the reference cooordinates system you want to
use here?
It could be
- the whole screen
- the whole browser application
- the workarea for application of the os
- the content area of the browser application
- the closest parent element
- the closest containing block as defined in CSS2
- the closest positioned relative within the containment hierarchy
etc...

I actually need the coordinates relative to the content area of the
browser application.

And thanks for the correction on the functions. Im also reading the
page you mentioned in your next post.
 
S

Seth Illgard

Quirksmode has an example with an explanation of how it works:

<URL:http://www.quirksmode.org/js/findpos.html>

The offsetParent strategy is not always correct. Matt Kruse has a
position library here with commented code:

<URL:http://www.javascripttoolbox.com/lib/objectposition/>

Hey Rob! Thanks for the links, specially for www.javascripttoolbox.com
.. That guy created a cool set of libraries that actually do some of
the functionallity im trying to implement. Thanks a lot for the links.
 
U

Une Bévue

Gérard Talbot said:
In the absolute, a (x, y) coordinate means nothing because it refers to
no known, defined, explicit coordinate system.

even God doesn't have an absolute coordinate ?

the reason why he doesn't hear me ?

;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top