PDA

View Full Version : div top/left values - simple question?


influx
03.02.2002, 19:04
Hi everyone,

This is hopefully a really simple question (from a newcomer, no less). I'm jumping back into DHTML after a bit of a hiatus, and I've apparently forgotten almost everything I used to know.

How do you, in standards-compliant fashion), retrieve the position of a div, the top and left values?

This is for a div which is being positioned initially by relative positioning - so getElementbyId("").style.left won't work.

Thanks in advance.

Brian Nickel
03.02.2002, 19:12
document.getElementById("...").offsetLeft

<font face='Courier New'><font color=brown>.Brian</font id=brown></font id='Courier New'>

Icestorm
03.02.2002, 19:29
Hi influx! Are you the same influx of yayhooray?

Anyway, the easy official way would be what you tried:

<pre id=code><font face=courier size=2 id=code><font color=maroon>
getElementbyId(id).style.left
</font id=maroon></font id=code></pre id=code>

But the style object only holds information if it has been set with js, or if the info is in the style attribute on the element.

Another W3C trick is getComputedStyle, see [url="http://dhtmlcentral.com/forums/topic.asp?TOPIC_ID=7703&FORUM_ID=11&CAT_ID=1&Topic_Title=getComputedStyle+%2F+getPropertyValue&Forum_Title=Standards"]this thread[/url:gm0o2jxtlk] for more info about that. The problem is that internet explorer doesn't support it. So you might write a function to combine it with [url="http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/currentstyle.asp"]currentStyle[/url:gm0o2jxtlk].

If you want to make things work quick and dirty, use the offset values. Supported by internet explorer, mozilla, opera and konqueror, as far I know. More info at [url="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/offsetleft.asp"]msdn[/url:gm0o2jxtlk].