【问题标题】:Getting infinite-scroll to trigger during scrolling, not just when buffer reached在滚动期间触发无限滚动,而不仅仅是在到达缓冲区时
【发布时间】:2012-12-02 18:26:05
【问题描述】:

我正在使用 Paul Irish 的“无限滚动”jquery 插件,它运行良好。

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

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

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

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

【问题讨论】:

    标签: javascript jquery infinite-scroll


    【解决方案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);
        }
    };
    

    解释:当您向下滚动页面(连续或不连续)时,'scroll' 事件会反复触发并调用上述代码中的handler: function(...)。在此方法内部,设置了一个计时器setTimeout,它将在触发滚动事件后 100 毫秒触发相应的无限滚动代码。然而,这个定时器也在方法{ clearTimeout(scrollTimeout); }中被取消了。

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

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

    【讨论】:

    • 非常感谢。我想我可以通过在新内容的 dom 内容加载之前保持“工作”标志打开来避免性能下降,在标志仍然打开时忽略任何进一步的滚动事件,而不是依赖计时器。它应该有望将开销保持在较低水平,并且仍能达到预期的效果。显然需要更多工具,但这是我一直在寻找的,再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 2018-04-20
    相关资源
    最近更新 更多