Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts

    offsetLeft offsetTop returning 1

    Hey,
    I need to find the top, left offset of a Div Tag that exists within a table.

    Example of the div in table:

    Code:
    <center><br><br>
    <table border="1">
      <tr>
        <td><div id="Layer1">test</div></td>
      </tr>
    <table>
    </center>
    using document.getElementById('Layer1').offsetLeft

    i return "1" always
    Last edited by rlemon; 04-18-2005 at 05:38 PM.

  • #2
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    figured it out.

    i need to set position: relative; in the div style.
    then use document.getElementById('id').offsetLeft

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    The offsetLeft and offsetTop are relative to the parent. To find out the absolute real position of an object you have to loop to all the parents, if exists

    PHP Code:
    function findPos(obj){//gets the absolute position of the objects
    var obj.offsetLeft;var 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;
    }
    }
    this.x=x;this.y=y;
    return 
    this.x;
    return 
    this.y;

    In the particular case of a table centered with HTML attribute, it is better to use <tbody> as an abject instead of <table> (otherwise Mozilla will return left 0)

    On the other hand it is useful to have always defined margins for body, to avoid differences in different browsers.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •