jQuery position problem with Chrome

Viewed 30774

When I alert the value .position().left, it returns 0 on Chrome. With other browsers it returns the actual number. Why does this happen?

8 Answers

Webkit can be too fast sometimes, but it's often taken care of in jQuery. You can debug using something like:

var v, elem = $('.myElement');
window.setTimeout(function() {
    v = elem.position().left;
    console.log(v);
    if (v) {
        return false;
    }
    window.setTimeout(arguments.callee, 1);
}, 1);

This will check if and when the position is available. If you are logging "0" in infinity, the position().left is "never" available and you need to debug elsewhere.

I had the same problem..

I fixed it using: .offset().left instead. But be aware that are not the same: http://api.jquery.com/offset/

.position().left worked in Chrome in some tests I did, using a similar approach than David (the value was available since the first try).

In my "real" application failed even reading the position on click event (which may eliminate any loading speed problem). Some comments (in other forum) say it may be related to the use of display:inline-block. However I couldn't reproduce the problem using inline-block. So it may be other thing.

use jQuery(window).load() instead of jQuery(document).ready()

Related