3

我有一个奇怪的问题,我不知道如何解决,想知道你们是否可以帮忙。

一点背景知识:我被要求创建一个系统,其中 wordpress 中页面的子页面以无限滚动的方式加载到该页面的末尾。这工作正常。

他们还希望顶部导航链接将所有内容加载到包括他们单击的页面,然后滚动到该页面。

如果我向下滚动(加载页面)然后单击顶部导航链接,则滚动正常工作。但是,如果我在单击其中一个链接之前没有加载更多页面,则页面将加载,并且滚动将开始,但只会在停止之前获得一些方式。这是由于 offset().top 给出的值不正确。我的问题是为什么?

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

您可以在此问题上给我的任何帮助将不胜感激!

4

0 回答 0