Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding Absolute position of items in page

Status
Not open for further replies.

DrButterfingers

Technical User
May 15, 2001
18
CA
Hi,

I am creating a web site and decided that it would be best to center it, thus equalizing the white space on both sides of the screen.

unfortuantely it is coming back to haunt me with the positioning of the dropdown menu buttons. The script I have uses absolute positioning and I would rather dynamically figure out the correct position that the menu buttons should be in,rather than use a series of conditional statements to catch "all" variations of screen display resolutions and browser percularities.

So! Is there a way that I could find the absolute position of a table or some other HTML tag relative to the top
(and/or left)of the screen?

Also does anybody know where I can find a complete list of all the object properties I can use with javascript?

Regards and thanks in advance,

Dr.Butterfingers

 
I got this from someone else on the forum. I can't remember who though:
Code:
function getCoordinates( obj ) {
    var x = 0; var y = 0;
    if( typeof obj == "string" ) {
        obj = document.getElementById( obj );
    }
    if( obj != null ) {
        while( obj.tagName != "BODY" ) {
            x += obj.offsetLeft;
            y += obj.offsetTop;
            obj = obj.offsetParent;
        }
        return [x,y];
    } else {
       return [0,0];
    }
}

function example(){
  var div=document.getElementById("myDiv");
  var coords=getCoordinates("myTarget");
  div.style.left=coords[0];
  div.style.top=coords[1];
}

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top