calculate div position

After trying out loads of ways to find out the position of div in web page I found this be the best code
function getPosition(obj){
var topValue= 0,leftValue= 0;
while(obj){
leftValue+= obj.offsetLeft;
topValue+= obj.offsetTop;
obj= obj.offsetParent;
}
finalvalue = {'left': leftValue, 'top' : topValue};
return finalvalue;
}

In order to use this just use the following

var curPos = getPosition(document.getElementById('divInQuestion'));
curPos.left is the postion left and curPos.top is the top position

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading ... Loading ...

About Nilesh Manohar

Web Developer
This entry was posted in Computers, HTML/CSS, Web Development and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>