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

pat*_*zdb 11 javascript jquery infinite-scroll

我正在使用infinitescroll.js脚本,它的效果非常好.我已经找到了如何使用以下代码替换默认功能和更多加载按钮:

$(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();
});
Run Code Online (Sandbox Code Playgroud)

但是,我想要做的是允许无限滚动运行3/4次然后显示.js-next-reports按钮.我不知道如何跟踪无限卷轴运行了多少次.我知道有一个currPagevar但是使用console.log我无法弄清楚我是如何引用它的.

maxPageinfinitescroll 还有一个选项,它限制它只运行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.',
        }
    },

});
Run Code Online (Sandbox Code Playgroud)

也许是这样的:?

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();
    });
}
Run Code Online (Sandbox Code Playgroud)

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

谢谢

小智 3

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');
        }
    }
});
Run Code Online (Sandbox Code Playgroud)