Web Analytics Made Easy -
StatCounter Dynamically Calculating Object Screen Positions - CodingForum

Announcement

Collapse
No announcement yet.

Dynamically Calculating Object Screen Positions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.
    'If you don't stand for something, you'll fall for anything.'

    Comment


    • #3
      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; Jul 10, 2002, 06:51 PM.

      Comment

      Working...
      X