code_junkie

Get the position of a div/span tag

Nov 12th, 2011
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. <span id='11a' style='top:55px;' onmouseover="GetPos(this);">stuff</span>
  2. <span id='12a' onmouseover="GetPos(this);">stuff</span>
  3.  
  4. document.getElementById('11a').style.top
  5.  
  6. function getPos(el) {
  7. // yay readability
  8. for (var lx=0, ly=0;
  9. el != null;
  10. lx += el.offsetLeft, ly += el.offsetTop, el = el.offsetParent);
  11. return {x: lx,y: ly};
  12. }
  13.  
  14. var x = el.offsetLeft, y = el.offsetTop;
  15.  
  16. var yPositionOfNewElement = el.offsetTop + el.offsetHeight + someMargin;
  17.  
  18. var left = $('.span').offset().left,
  19. top = $('.span').offset().top;
  20.  
  21. $(document).ready(function(){
  22. // Alert the position with jQuery
  23. var position = $("#myDivInQuestion").position();
  24. alert('X: ' + position.left + ", Y: " + position.top );
  25. });
Add Comment
Please, Sign In to add comment