JavaScript Finding Element Position Inaccurate
Posted: Fri Feb 22, 2008 2:04 pm
The consensus for finding a element's position, as per quirksmode.org and other sources, is to continue to get the parent and add the offsetTop and offsetLeft until the top most element is reached and there is no longer any parent element.
However, the problem with this algorithm is that if the parent is absolute positioned, then the calculated offsetTop and offsetLeft, will be referenced from that element instead of HTML. Therefore, the algorithm needs to be corrected to test whether the element's parent is absolute positioned and stop because it doesn't need to reference the parent of that element.
The problem, I'm encountering is that testing the style attribute "position" value doesn't give me any results.
If you it returns an empty string. Which I'm unsure what the cause of that is. The style is inline to the element, if case that matters. Also, the code is in a function in an external script referenced in the head element. The function is called by adding a hover JavaScript event to the element.
So I need help on what I'm doing wrong with getting the position and the contents of the position style attribute.
However, the problem with this algorithm is that if the parent is absolute positioned, then the calculated offsetTop and offsetLeft, will be referenced from that element instead of HTML. Therefore, the algorithm needs to be corrected to test whether the element's parent is absolute positioned and stop because it doesn't need to reference the parent of that element.
The problem, I'm encountering is that testing the style attribute "position" value doesn't give me any results.
Code: Select all
var positiontype = obj.style.position;
if( positiontype == "absolute" )
return [height, width];
Code: Select all
alert(positiontype)So I need help on what I'm doing wrong with getting the position and the contents of the position style attribute.