3

I'm having a bizarre issue that I don't know how to solve and was wondering if you guys could help.

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.

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

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 ?

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 <= 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;
}​

Any help you could give me on this issue would be really appreciated!

juan.facorro
  • 9,791
  • 2
  • 33
  • 41
Hugo Firth
  • 535
  • 5
  • 13
  • 2
    How 'wrong' is it? Bear in mind that `offset()` returns the offset relative to the page/document, not relative to the offset-parent (`position()`). – David Thomas Sep 05 '12 at 17:39
  • 2
    Significantly - sometimes as much as 2000px off. Someone has pointed out that its calculating the offset before images etc... are loaded into the page, which will have a big effect. Is there a way round this ? – Hugo Firth Sep 05 '12 at 17:53
  • I had a similar issue where it was calculating the offset BEFORE the images were loaded, so i just wrapped my code in a load function that would execute after my banner image was loaded! – kiaaanabal Aug 27 '14 at 01:28

0 Answers0