{"id":2479,"date":"2016-05-19T18:05:27","date_gmt":"2016-05-20T01:05:27","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=2479"},"modified":"2016-05-19T18:05:27","modified_gmt":"2016-05-20T01:05:27","slug":"getting-the-position-of-elements-on-the-screen","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=2479","title":{"rendered":"Getting the position of elements on the screen"},"content":{"rendered":"<p>I have a game where I move a target and drop it on foils. I know where I put the foils, but I wanted to know where they are in relation to the window if the window is scrolled or resized. This isn\u2019t exactly what I was needing, but it\u2019s useful enough that I might need it in the future. <\/p>\n<p><pre><code class=\"preserve-code-formatting\">\n\/\/ Find the position of elements\nfunction getOffset(element) {\n&nbsp;&nbsp;&nbsp;&nbsp;element = element.getBoundingClientRect();\n&nbsp;&nbsp;return {\n&nbsp;&nbsp;&nbsp;&nbsp;left: element.left + window.scrollX,\n&nbsp;&nbsp;&nbsp;&nbsp;right: element.right + window.scrollX,\n&nbsp;&nbsp;&nbsp;&nbsp;top: element.top + window.scrollY,\n&nbsp;&nbsp;&nbsp;&nbsp;bottom: element.bottom + window.scrollY\n&nbsp;&nbsp;}\n}\nfunction whereAreTheFoils() {\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Find out where things are for initial setup and debugging\n&nbsp;&nbsp;&nbsp;&nbsp;var foil0 = document.getElementById(&#039;foil_0&#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;var foil1 = document.getElementById(&#039;foil_1&#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;var foil2 = document.getElementById(&#039;foil_2&#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;var foil3 = document.getElementById(&#039;foil_3&#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;console.log(&#039;Foil 0 is at LR &#039; + getOffset(foil0).left + &#039;, &#039; + getOffset(foil0).right );\n&nbsp;&nbsp;&nbsp;&nbsp;console.log(&#039;Foil 0 is at TB &#039; + getOffset(foil0).top + &#039;, &#039; + getOffset(foil0).bottom );\n\n&nbsp;&nbsp;&nbsp;&nbsp;console.log(&#039;Foil 1 is at &#039; + getOffset(foil1).left + &#039;, &#039; + getOffset(foil1).top );\n&nbsp;&nbsp;&nbsp;&nbsp;console.log(&#039;Foil 2 is at &#039; + getOffset(foil2).left + &#039;, &#039; + getOffset(foil2).top );\n&nbsp;&nbsp;&nbsp;&nbsp;console.log(&#039;Foil 3 is at &#039; + getOffset(foil3).left + &#039;, &#039; + getOffset(foil3).top );\n}<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a game where I move a target and drop it on foils. I know where I put the foils, but I wanted to know where they are in relation to the window if the window is scrolled or resized. This isn\u2019t exactly what I was needing, but it\u2019s useful enough that I might &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=2479\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Getting the position of elements on the screen<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[],"class_list":["post-2479","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2479","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2479"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2479\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}