RSS LOGORss   Welcome Guest  Login Register
Welcome to our Blogs
Back Back
Subject


mag
2008/08/25 02:34AM
Finding the Position of an Element      Number of views: 1766

Yesterday, I was searching for a piece of javascript that can find an element's position in a page, and I have found this (sorry, I don't have the link to the source, but, I'll add it later)

 

var xPos=0;
var yPos = 0;

function findPos(obj){

xPos = obj.offsetLeft;

yPos = obj.offsetTop;

 

while  (obj.offsetParent){

  xPos=xPos+obj.offsetParent.offsetLeft;

  yPos =yPos+obj.offsetParent.offsetTop;

 

  if (obj==document.getElementsByTagName( 'body' )[0])
    { break}
  else
    {obj=obj.offsetParent;}

   }
}

 


you can call it just by passing the element by itself, you can use "document.getElementById('the object id');" to get the reference of your object,

 

 

Enjoy,

 

Microsoftawy :)