nhhockeyplayer wrote: > > Does anyone have a reliable method for calculating x,y ? > My code lives in a *.script file > I have these but they are off by 40 or so pixels for both x and y. > I have seen variations on this method several times, but they are all fundamentally flawed because they don't take into account if the element is a child of a DOM element with position:fixed, nor do they account for margins on elements with position:absolute.
I don't believe this is 100% cross-browser-compatible, but should at least get you going down the right path (works great in Firefox, naturally). If HTMLElement isn't declared in whatever browser you're using, you'll have to write a stand-alone function. This is just a lot cleaner. Usage: DOMElement.getXY() Returns: {x, y} Example: alert("Element foo's x position:" + document.getElementById("foo").getXY().x); HTMLElement.prototype.getXY = function() { var x = 0, y = 0, obj; obj = this; do { var cs = document.defaultView.getComputedStyle(obj, null); if (cs.getPropertyValue("position") == "fixed") { // do nothing } else { x += obj.offsetLeft || 0; y += obj.offsetTop || 0; if (cs.getPropertyValue("position") == "absolute") { x += (0 - parseInt(cs.getPropertyValue("margin-left"), 10)); y += (0 - parseInt(cs.getPropertyValue("margin-top"), 10)); } } obj = obj.offsetParent; } while (obj); return {x: x, y: y}; } -- View this message in context: http://www.nabble.com/T-4.1.6%2C-anyone-got-a-good-getPosition%28node%29-function-tp20003483p20036664.html Sent from the Tapestry - User mailing list archive at Nabble.com.