View Full Version : problem with x and y position of the layers
kriskishi
04-15-2005, 04:43 PM
hi
i have got the problem in finding the layers x and y positions
i am getting the number of div layers by using the code
document.getElementsByTagName("div").length
but when i am trying to get the position of "layer1" with id "first" with this code
document.getElementsByTagName("div").id.x
i am not getting the value.
kishore
the object's position is to be found:
1. if the object has already been postioned absolute by a CSS or a on-the-fly method
obj.style.left
obj.style.top
2. If not, the true absolute position is to be found using offset position attributes. Well, they are relative to the parent, if any, so that you might use something like that:
var x; var y;
function findPos(obj){
x=obj.offsetLeft;
y=obj.offsetTop;
while(obj.offsetParent){
if(obj==document.getElementsByTagName('body')[0]){break;}
else{
x=x+obj.offsetParent.offsetLeft;
y=y+obj.offsetParent.offsetTop;
obj=obj.offsetParent;
}
}
}
make sure that the margins (at least left and top) of the body are 0px.
NOTE: offset attributes are readonly
Thinking a little bit, I guess that you have used the term position in an unfortunate way. Probably you wanna say index
If so, to return the index of an element in a collection upon it's id, I think you must loop through the collection;
var divs = document.getElementsByTagName('div');
for(var i=0;i<divs.length;i++){
if(divs[i].getAttribute('id')=='myid'){
alert('the index of the object is: '+i);break;
}
}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.