Get and set position and size of html nodes

B

Benjamin Kalytta

Hi there,

How do I get and set position and size of a html node (certain elements)?

Following will not work for non absolute elements:

document.defaultView.getComputedStyle(element,null).getPropertyValue("left")
/ getPropertyValue("width") ...

It will return "auto" in certain circumstances.

Using offsetLeft / offsetWidth to get the size and position seems also
not to be the best since values sometimes seems to be incorrect. Setting
control width to offsetWidth will causes the control to get larger than
before.

What is the best way to do this?

Is there any open source javascript "library" (js file) for this purpose?

yours
Benjamin
 
R

RobG

Hi there,

How do I get and set position and size of a html node (certain elements)?

Following will not work for non absolute elements:

document.defaultView.getComputedStyle(element,null).getPropertyValue("left")
/ getPropertyValue("width") ...

It will return "auto" in certain circumstances.

Using offsetLeft / offsetWidth to get the size and position seems also
not to be the best since values sometimes seems to be incorrect. Setting
control width to offsetWidth will causes the control to get larger than
before.

What is the best way to do this?

Is there any open source javascript "library" (js file) for this purpose?

Have a look at Matt Kruse's position library in his Javascript
Toolbox:

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

Peter Michaux

Have a look at Matt Kruse's position library in his Javascript
Toolbox:

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

Matt's is a good one for position reporting when the page is simple.
My tests showed it doesn't work in all combinations of nested elements
where some of the elements are scrolling, tables, frames, and other
oddities in a wide range of browsers (IE6, O8, S2, FF1.5, I believe).
I haven't seen a library that can do it all. Richard Cornford probably
has some of the most complete knowledge in this area and contests
repeatedly that such a library would be huge.

Peter
 

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
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top