.offset().top returning the wrong value
<p>I'm having a bizarre issue that I don't know how to solve and was wondering if you guys could help.</p>

<p>A bit of background: I have been asked to create a system where subpages of a page in wordpress are loaded on the end of that page in an infinite scroll. This is working correctly. </p>

<p>They also want the top nav links to load all content upto and including the page they clicked and then scroll to it. </p>

<p>If I scroll down (loading the pages) and then click a top nav link the scroll works correctly. However if I load NO further pages before clicking one of the links, the pages will load, and the scroll will start, but will only get some of the way before stopping. This is due to an incorrect value being given by offset().top. My question is why ?</p>

<pre><code>function ajaxloadnscroll(index) {

    //If the page has already been loaded then just scroll to it
    if (pages[index].loaded) {
        $('html, body').animate({
            scrollTop: $("#" + pages[index].name).offset().top
        }, 2000);
        return;
    }

    //Loop through pages up to one clicked.
    for (i = 0; i &lt;= index; i++) {


        current = i;
        if (!pages[current].loaded) {
            $.ajax({
                url: pages[i].url,
                async: false,
                context: document.body,
                success: function(data) {
                    if (data) {
                        $("#tempload").before(data);
                        pages[current].loaded = true;

                        if (current == index) {
                            $('html, body').animate({
                                scrollTop: $("#" + pages[current].name).offset().top
                            }, 2000);
                        }

                    }
                }
            });
        }
    }

    //Increment current in order to load next page object on scroll.
    current++;
    return false;
}​
</code></pre>

<p>Any help you could give me on this issue would be really appreciated!</p>
