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 Coder
    Join Date
    Mar 2004
    Posts
    91
    Thanks
    0
    Thanked 0 Times in 0 Posts

    bug in calculating the position of an element

    Hello

    I wrote a function which will find the exact top left coordinates in the screen.
    The function will return an associative array with information on the element:
    Code:
    function getPositionAndDimension(element) {
      var left = 0;
      var top = 0;
      var width = element.offsetWidth;
      var height = element.offsetHeight;
      while (element.parentElement != null) {
        left += element.offsetLeft;
        top += element.offsetTop;
        element = element.parentElement;
      }
    	
      var results = new Array(4);
      results['left'] = left;
      results['top'] = top;
      results['width'] = width;
      results['height'] = height;
    		
      return results;
    }
    The problem is that the value that the function calculates for the element's top is incorrect.
    I am getting values that are much larger than the actual top value.

    can anyone see where the bug is in?
    I am testing this on IE6

    thanks

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Ames, IA, USA
    Posts
    373
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Have you tried offsetParent instead of parentElement?
    Need more emoticons?
    Visit Catman's Private Stock

  • #3
    New Coder
    Join Date
    Mar 2004
    Posts
    91
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes, that did the trick.
    thanks


  •  

    Posting Permissions

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