允许 infinitescroll.js 到 运行 X 次,然后加载更多帖子

Allow infinitescroll.js to run X times, then load more posts

我正在使用 infinitescroll.js script and it works really well. I've found out how to replace the default functionality with a load more 按钮,使用此代码:

$(window).unbind('.infscr');
$('.js-next-reports').click(function() {
    $grid.infinitescroll('retrieve');
    return false;
});
$(document).ajaxError(function(e, xhr, opt) {
    if (xhr.status == 404) $('.js-next-reports').remove();
});

但是,我想做的是允许无限滚动到 运行 3/4 次,然后显示 .js-next-reports 按钮。不过,我不确定如何跟踪无限滚动有多少次 运行。我知道有一个 currPage var 但使用 console.log 我不知道如何引用它。

无限滚动还有一个 maxPage 选项,它将它限制为 运行 只有 X 次,所以我也许可以以某种方式利用它?不过,我不确定如何获得 console.log 选项。如果有帮助,这是我的初始化代码($grid 只是对 div)

的引用
$grid.infinitescroll({

    // selector for the paged navigation (it will be hidden)
    navSelector  : ".pagination",
    // selector for the NEXT link (to page 2)
    nextSelector : ".pagination .next",
    // selector for all items you'll retrieve
    itemSelector : ".infinite-scroll-post",
    contentSelector : "#infinite-scrollable",
    debug: true,

    // finished message
    loading: {
        img: "ajax-loader.gif",
        msgText: "Loading more projects...",
        finishedMsg: 'No more pages to load.',
        }
    },

});

可能是这样的:?

if ( .currPage == "3" ) {
    $(window).unbind('.infscr');
    $('.js-next-reports').click(function() {
        $grid.infinitescroll('retrieve');
        return false;
    });
    $(document).ajaxError(function(e, xhr, opt) {
        if (xhr.status == 404) $('.js-next-reports').remove();
    });
}

但我不知道如何计算卷轴或访问 currPage

谢谢

JSFiddle 将有助于测试代码,但根据我在他们的文档中阅读的内容,有一个回调允许您访问状态对象内的 currPage。您的代码应如下所示:

$grid.infinitescroll({

    // selector for the paged navigation (it will be hidden)
    navSelector  : ".pagination",
    // selector for the NEXT link (to page 2)
    nextSelector : ".pagination .next",
    // selector for all items you'll retrieve
    itemSelector : ".infinite-scroll-post",
    contentSelector : "#infinite-scrollable",
    debug: true,

    // finished message
    loading: {
        img: "ajax-loader.gif",
        msgText: "Loading more projects...",
        finishedMsg: 'No more pages to load.',
    },
    appendCallback: false
    }, function(newitems, opts) {
        if(opts.state.currPage == 3) {
            $(window).unbind('.infscr');
        }
    }
});