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
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Dynamically Calculating Object Screen Positions

    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

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    This little Earth.
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    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:

    Code:
    var pos=getPos("elementID");
    alert(pos.y);
    alert(pos.x);
    Hope that helps!

    Happy coding!
    Last edited by nolachrymose; 07-10-2002 at 11:51 PM.


  •  

    Posting Permissions

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