www.webdeveloper.com
Results 1 to 4 of 4

Thread: Page element position failing on chrome

Hybrid View

  1. #1
    Join Date
    Feb 2007
    Posts
    21

    Page element position failing on chrome

    Hi,

    I use this to get the position of a page element:

    Code:
        var _x = 0; 
        var _y = 0; 
        while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) { 
            _x += el.offsetLeft - el.scrollLeft; 
            _y += el.offsetTop - el.scrollTop; 
            el = el.offsetParent; 
        }
    This works on all browsers except Chrome. Chrome seems to return a value also based on scroll position. Is this right? This because searching around, something like the above code is generally considered to be crossbrowser compatible. But I cannot find much information about this regarding Chrome.

    Thanks!

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Just remove:

    Code:
    - el.scrollLeft;
    - el.scrollTop;
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Feb 2007
    Posts
    21
    So indeed chrome is handling this different then the other browsers and there is no code that works on all browsers except from jquery .offset?

  4. #4
    Join Date
    Mar 2009
    Posts
    452
    i would recommend jquery as you will not have to worry much about different browsers.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles