...

View Full Version : Dynamically Calculating Object Screen Positions



spaceman
07-10-2002, 05:14 PM
Is there any way of dynamically picking an html element on the page through the DOM and calculating it's screen position that works in Netscape (4 and 6) and IE?

Thanks in advance for any help.

Ian

ASAAKI
07-10-2002, 11:55 PM
a guess...just for IE....

var el=document.getElementById('your_element');
var x=el.left;
var y=el.top;

for x and y to store the coordinates of the top left corner of the element.

nolachrymose
07-11-2002, 12:46 AM
As far as I know those two properties return values relative to the browser frame and not the entire screen. To use the screen as the reference object use this code instead:


function getPos(obj_id) {
var el;
if(typeof(document.getElementById)!="undefined")
el=document.getElementById(obj_id).style;
else if(typeof(document.all)!="undefined")
el=document.all[obj_id].style;
else if(document.layers!=null) //not sure if NN 4 supports typeof
el=document.layers[obj_id];

var pos=new Object();
var pos.x=el.left+screen.width-screen.availWidth;
var pos.y=el.top+screen.height-screen.availHeight;

return pos;
}

Then, you can get an element's position relative to the screen like so:


var pos=getPos("elementID");
alert(pos.y);
alert(pos.x);

Hope that helps!

Happy coding! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum