0

我正在使用 Paul Irish 的 'infinite-scroll' jquery 插件,它运行良好。

我想在预发射时非常激进,所以我将我的bufferPx参数设置为1000.

当我向下滚动足够远时它会触发,但是它只会在滚动停止后触发,如果我轻弹滚轮并在滚轮停止之前到达底部,则更新不会触发,直到我击中底部离开我等待下一个结果集出现。

我想要的行为是在我仍在积极滚动时触发更新,这样如果我快速浏览我的列表,我就不必等待一旦我到达底部。

有谁知道如何做到这一点,我可以添加一个新的事件触发器,如果​​是这样,当滚动仍然处于活动状态时我会使用什么触发器?

4

1 回答 1

1

查看插件的源代码https://raw.github.com/paulirish/infinite-scroll/master/jquery.infinitescroll.js,我会说您需要修改以下代码:

event.special.smartscroll = {
    setup: function () {
        $(this).bind("scroll", event.special.smartscroll.handler);
    },
    teardown: function () {
        $(this).unbind("scroll", event.special.smartscroll.handler);
    },
    handler: function (event, execAsap) {
        // Save the context
        var context = this,
        args = arguments;

        // set correct event type
        event.type = "smartscroll";

        if (scrollTimeout) { clearTimeout(scrollTimeout); }
        scrollTimeout = setTimeout(function () {
            $.event.handle.apply(context, args);
        }, execAsap === "execAsap" ? 0 : 100);
    }
};

在此行上进行编辑}, execAsap === "execAsap" ? 0 : 100);

event.special.smartscroll = {
    setup: function () {
        $(this).bind("scroll", event.special.smartscroll.handler);
    },
    teardown: function () {
        $(this).unbind("scroll", event.special.smartscroll.handler);
    },
    handler: function (event, execAsap) {
        // Save the context
        var context = this,
        args = arguments;

        // set correct event type
        event.type = "smartscroll";

        if (scrollTimeout) { clearTimeout(scrollTimeout); }
        scrollTimeout = setTimeout(function () {
            $.event.handle.apply(context, args);
        }, 0);
    }
};

说明:当您向下滚动页面(连续或离散)时,“滚动”事件会一遍又一遍地触发并调用handler: function(...)上面代码中的 。在此方法内部,设置了一个计时器,该计时器setTimeout将在触发滚动事件 100 毫秒后触发相应的无限滚动代码。但是,这个计时器也在方法内部被取消{ clearTimeout(scrollTimeout); }

因此,只要用户继续滚动,计时器就会不断清零并重新设置,直到用户停止滚动。在最后一个滚动事件中,计时器被设置并且永远不会被清除,因此它将在 100 毫秒后触发并执行内容加载。我在上面演示的编辑删除了这个等待期并在用户到达页面底部时加载内容(即使他们仍在积极滚动)。

注意:作者可能打算将此超时用于性能问题,但欢迎您在配置中对其进行测试。

于 2012-12-03T04:44:43.673 回答