{"id":192,"date":"2009-07-07T00:00:44","date_gmt":"2009-07-06T16:00:44","guid":{"rendered":"http:\/\/leegorous.net\/blog\/?p=192"},"modified":"2012-03-03T20:33:44","modified_gmt":"2012-03-03T12:33:44","slug":"how-to-get-element-absolute-position-in-javascript","status":"publish","type":"post","link":"http:\/\/leegorous.net\/blog\/2009\/07\/07\/how-to-get-element-absolute-position-in-javascript\/","title":{"rendered":"JavaScript\u4e2d\u83b7\u53d6\u5143\u7d20\u7684\u7edd\u5bf9\u4f4d\u7f6e"},"content":{"rendered":"<p>\u5728\u4e00\u4e9b\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u4f1a\u6709\u9700\u8981\u77e5\u9053\u67d0\u4e00\u4e2a\u63a7\u4ef6\u5728\u9875\u9762\u4e2d\u7684\u4f4d\u7f6e\uff0c\u5728\u7f51\u4e0a\u6bd4\u8f83\u5bb9\u6613\u627e\u5230\u4e0b\u9762\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<p>\u5728\u9875\u9762\u4e2d\u6709\u4e00\u4e2a\u6309\u94ae\uff1a<\/p>\n<pre name=\"code\" class=\"brush: html\">\r\n&lt;input id=a type=&quot;button&quot; \r\n    value=&quot;click me to get position&quot; \r\n    onclick=&quot;getPos(this)&quot;\/&gt;\r\n<\/pre>\n<p>\u5728\u811a\u672c\u4e2d\u54cd\u5e94\u70b9\u51fb\u4e8b\u4ef6\u7684\u662f\u8fd9\u4e2a\u51fd\u6570\uff1a<\/p>\n<pre name=\"code\" class=\"brush: js\">\r\nfunction getPos(e){\r\n    var t=e.offsetTop;\r\n    var l=e.offsetLeft;\r\n    while(e=e.offsetParent){\r\n        t+=e.offsetTop;\r\n        l+=e.offsetLeft;\r\n    }\r\n    alert(&quot;top=&quot;+t+&quot;\\nleft=&quot;+l);\r\n}\r\n<\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u7684\u5b9e\u73b0\u76f8\u5f53\u7cbe\u70bc\uff0c\u610f\u601d\u5c31\u662f\u5148\u53d6\u5f97\u81ea\u5df1\u7684\u76f8\u5bf9\u4f4d\u7f6e\uff0c\u518d\u53e0\u52a0\u5176\u6700\u8fd1\u7684\u76f8\u5bf9\u4f4d\u7f6e\u5bb9\u5668\uff08offsetParent\uff0c\u5b83\u4e0d\u4e00\u5b9a\u662f\u5176\u7236\u8282\u70b9\uff09\u7684\u76f8\u5bf9\u4f4d\u7f6e\uff0c\u76f4\u81f3\u9876\u5c42\u4f4d\u7f6e\u5bb9\u5668\uff08\u4e00\u822c\u5c31\u662fbody\uff09\uff0c\u4ece\u800c\u5f97\u51fa\u8be5\u8282\u70b9\u7684\u76f8\u5bf9\u4f4d\u7f6e\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0c\u5f88\u5feb\u5c31\u53d1\u73b0\u8fd9\u4e2a\u51fd\u6570\u4e5f\u8bb8\u5e76\u4e0d\u591f\u7528\uff0c\u56e0\u4e3a\u6211\u5728\u9875\u9762\u91cc\u6709\u53ef\u80fd\u4f7f\u7528\u4e86\u4e00\u4e9bCSS\u6765\u4f7f\u5f97\u672c\u6765\u5e73\u94fa\u7684\u753b\u9762\u53d8\u6210\u4e00\u4e2a\u6eda\u52a8\u533a\u57df\uff0c\u4f8b\u5982\u8bbe\u7f6e\u4e86\u7236\u8282\u70b9\u7684height\u4e3a\u67d0\u4e2a\u503c\uff0c\u5e76\u4e14\u5176overflow\u8bbe\u4e3aauto\u6216\u8005scroll\u3002\u8fd9\u65f6\uff0c\u4e0a\u9762\u7684\u65b9\u6cd5\u56e0\u4e3a\u6ca1\u6709\u8ba1\u7b97\u5176\u6eda\u52a8\u504f\u79fb\uff0c\u6240\u4ee5\u6240\u5f97\u7684\u503c\u4e0d\u4e00\u5b9a\u662f\u5143\u7d20\u5f53\u524d\u7684\u7edd\u5bf9\u4f4d\u7f6e\uff0c\u6240\u4ee5\u6211\u5bf9\u4e0a\u9762\u7684\u65b9\u6cd5\u8fdb\u884c\u4e86\u4e00\u4e9b\u5c0f\u6539\u52a8\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u52a0\u5165\u4e86\u5bf9\u5176\u6eda\u52a8\u504f\u79fb\u91cf\u7684\u8ba1\u7b97\u3002<\/p>\n<pre name=\"code\" class=\"brush: js\">\r\nfunction getPosition(sender) {\r\n    var e=sender,E=e;\r\n    var x=e.offsetLeft;\r\n    var y=e.offsetTop;\r\n    while (e=e.offsetParent) {\r\n        var P=e.parentNode;\r\n        while (P!=(E=E.parentNode)) {\r\n            x-=E.scrollLeft;\r\n            y-=E.scrollTop;\r\n        }\r\n        x+=e.offsetLeft;\r\n        y+=e.offsetTop;\r\n        E=e;\r\n    }\r\n    return {&quot;x&quot;:x,&quot;y&quot;:y};\r\n    \/\/alert(&quot;top=&quot;+y+&quot;\\nleft=&quot;+x);\r\n}\r\n<\/pre>\n<p>\u7a0d\u5fae\u6d4b\u8bd5\u4e86\u4e00\u4e0b\uff0c\u57fa\u672c\u4e0a\u5728IE\u548cFF\u4e2d\u90fd\u80fd\u6b63\u5e38\u8fd0\u8f6c\u3002<\/p>\n<p>\u5728\u8fd0\u7528\u65b9\u9762\uff0c\u7531\u4e8e\u4f7f\u7528\u5230\u5f80\u4e0a\u904d\u5386\uff0c\u5982\u679c\u8282\u70b9\u6811\u7ed3\u6784\u8fc7\u4e8e\u590d\u6742\uff0c\u800c\u4e14\u6709\u4e0d\u4f1a\u6709\u6eda\u52a8\u51fa\u73b0\u7684\u8bdd\uff0c\u90a3\u4e48\u8fd8\u662f\u7528\u7b2c\u4e00\u4e2a\u65b9\u6cd5\u5c31\u8db3\u591f\u4e86\u3002<\/p>\n<p>\u6ce8\uff1a\u53c8\u662f\u7092\u51b7\u996d -_-! \u539f\u4f5c\u53d1\u8868\u4e8e 2007-09-08<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e00\u4e9b\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u4f1a\u6709\u9700\u8981\u77e5\u9053\u67d0\u4e00\u4e2a\u63a7\u4ef6\u5728\u9875\u9762\u4e2d\u7684\u4f4d\u7f6e\uff0c\u5728\u7f51\u4e0a\u6bd4\u8f83\u5bb9\u6613\u627e\u5230\u4e0b\u9762\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6cd5\u3002 \u5728\u9875\u9762\u4e2d\u6709\u4e00\u4e2a\u6309 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/leegorous.net\/blog\/2009\/07\/07\/how-to-get-element-absolute-position-in-javascript\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cJavaScript\u4e2d\u83b7\u53d6\u5143\u7d20\u7684\u7edd\u5bf9\u4f4d\u7f6e\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[102],"tags":[33,9],"_links":{"self":[{"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/posts\/192"}],"collection":[{"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/comments?post=192"}],"version-history":[{"count":5,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/posts\/192\/revisions"}],"predecessor-version":[{"id":1070,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/posts\/192\/revisions\/1070"}],"wp:attachment":[{"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/media?parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/categories?post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/tags?post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}