calculate div position
Posted in Computers, HTML/CSS, Web Development
Tagged with javascript
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




