{"id":97,"date":"2009-06-03T20:25:39","date_gmt":"2009-06-03T14:55:39","guid":{"rendered":"http:\/\/vikku.info\/codesnippets\/?p=97"},"modified":"2009-06-03T20:31:04","modified_gmt":"2009-06-03T15:01:04","slug":"find-absolute-position-of-an-html-element","status":"publish","type":"post","link":"http:\/\/vikku.info\/codesnippets\/javascript\/find-absolute-position-of-an-html-element\/","title":{"rendered":"find absolute position of an html element"},"content":{"rendered":"<p>Html elements are displayed cascading by default. So every element appears one after the other and it is the same with nested elements. I mean elements within another element. <\/p>\n<p>I want to find the exact absolute position  of an element. In other words i want to find the exact x,y of an element in the html page if you could consider the html page as a graph.<\/p>\n<p>I used left and top but it will always give the x and y with respect to the parent and not to the window or the body tag which is the first parent for all visible elements. Then i read about the property offsetLeft and offsetTop but this too is with respect to the parent.<\/p>\n<p>So find the parents left + its parent left + its parent till you reach the top then you will get the left of any element. The same way for finding the top that is y. here is the function which will give you the x,y\/absolute position of any element. <\/p>\n<p>But when i used float left property i found this function returned a different value so please check when using float left option. anyway &#8230;<\/p>\n<pre lang=\"javascript\">\r\nfunction findAbsolutePosition(obj) {\r\n\tvar curleft = curtop = 0;\r\n\tif (obj.offsetParent) {\r\n\t\tdo {\r\n\t\t\tcurleft += obj.offsetLeft;\r\n\t\t\tcurtop += obj.offsetTop;\r\n\t\t} while (obj = obj.offsetParent);\r\n\t}\r\n\treturn [curleft,curtop];\r\n\/\/returns an array\r\n}\r\n\r\n<\/pre>\n<p>&#8230;<br \/>\nand to my surprise i found the following link when i tried to find something like this to check my work.<\/p>\n<p><a href=\"http:\/\/www.quirksmode.org\/js\/findpos.html\" target=\"_blank\">http:\/\/www.quirksmode.org\/js\/findpos.html<\/a><\/p>\n<p>I use this site to cross check my snippets in javascript if present. This is one useful site i have found which analyses beyond the scope.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Html elements are displayed cascading by default. So every element appears one after the other and it is the same with nested elements. I mean elements within another element. I want to find the exact absolute position of an element. In other words i want to find the exact x,y of an element in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/97"}],"collection":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/comments?post=97"}],"version-history":[{"count":6,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/97\/revisions"}],"predecessor-version":[{"id":103,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/97\/revisions\/103"}],"wp:attachment":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/media?parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/categories?post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/tags?post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}