I need to get absolute coordinates (left and top relative to beginning of document) of any DOM element inspected by Firebug regardless depth of DOM tree. Is there such a plugin?
I need to get absolute coordinates (left and top relative to beginning of document)
Share
You don’t need a plugin in fact, just a chunk of JavaScript + knowledge of some Firebug internals.
You can find in this article how to get absolute coords of a DOM element. Each DOM element has properties
.offsetLeft .offsetTop .offsetParentthat define the position regarding some other DOM element (the offsetParent). The element on the top of hierarchy has aoffsetParent = null. You can traverse the elements and its offsetParents up the hierarchy to find the absolute coords, given an element.The code copied from that page:
(paste it in Firebug, and then you have a
findposfunction available in the console).For majority of simplest cases, you will have
offsetParent = <body>which has zero offsets (like in the screenshots) so you don’t need to add anything to offsetLeft and offsetTop. However if relative positionings take place, then you must traverse the parents.When you select an element in Firebug, then it’s available as
$0in Firebug console.So, after mixing the two things, you can issue the commands like in the screenshot:
to easily get absolute coords of any element.
The script I’ve pasted above should work in the huge majority of occasions. You may want to read this post also for a bit more robust function.