element top, left position


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function getElementTopLeft(id) {
  2.  
  3. var ele = document.getElementById(id);
  4. var top = 0;
  5. var left = 0;
  6.  
  7. while(ele.tagName != "BODY") {
  8. top += ele.offsetTop;
  9. left += ele.offsetLeft;
  10. ele = ele.offsetParent;
  11. }
  12.  
  13. return { top: top, left: left };
  14.  
  15. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.