recursively checks every parent of the element to get the ABSOLUTE offset of the element.
call it like this:
getOff(element).x
getOff(element).y
| function(a,x,y){ | |
| for(x=y=0;a;a=a.offsetParent) //Loop through element and all it's parents | |
| x+=a.offsetLeft, //add offset of current element to variables x and y | |
| y+=a.offsetTop; | |
| return{x:x,y:y} //return object of x and y | |
| } |
| function(a,x,y){for(x=y=0;a;a=a.offsetParent)x+=a.offsetLeft,y+=a.offsetTop;return{x:x,y:y}} |
| { | |
| "name": "getOff", | |
| "description": "returns the ABSOLUTE offset of an element", | |
| "keywords": [ | |
| "css", | |
| "position", | |
| "offset", | |
| "absolute" | |
| ] | |
| } |
| <!doctype html> | |
| <style> | |
| #foo{position:relative;top:100px;margin:40px} | |
| </style> | |
| <div id=foo></div> | |
| <script> | |
| getOff = function(a,x,y){for(x=y=0;a;a=a.offsetParent)x+=a.offsetLeft,y+=a.offsetTop;return{x:x,y:y}}; | |
| document.write( | |
| getOff(document.getElementById("foo")).y | |
| ); | |
| </script> |
This reminds me too much of the ambigious old actionscript syntax... I'll rather take the object, thanks.