“无限scroll"代码只适用于顶部滚动

"infinite scroll" code working only for top scrolling

本文关键字:适用于 顶部 滚动 代码 无限 scroll quot      更新时间:2023-09-26

我正在使用一个"通用"的js片段,它应该检测用户是否滚动到文档的底部:

 $(window).scroll(function() {
        if ($(window).scrollTop() == $(document).height() - $(window).height()) {
            //ajax code here
        }
    });

当用户滚动时,新的内容应该通过ajax调用加载。

我本来希望代码在我向下滚动时触发,但实际上当我滚动回页面顶部时,条件才触发,所以它基本上与它"应该"做的相反。

我在很多例子中都看到过这种解决方案,比如:https://stackoverflow.com/a/17078097/1623095

我的调试信息:

console.log($(window).scrollTop());
console.log($(document).height());
console.log($(window).height());

滚动到顶部时的输出:

0 
1956 
1956 

和底部:

961 
1956
1956 

唯一的插件正在加载在我的页面是jquery (1.10.0.min),由于项目的性质,我不能链接到页面。

完全被这里的dom行为搞糊涂了

我以前为别人解决过这个问题。

看这里:

$(window).scroll(function () {
        //- 10 = desired pixel distance from the bottom of the page while scrolling)
        if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) {
            var box = $("#scrollbox");
            //Just append some content here
            box.html(box.html() + "<br />fa");
        }
});

小提琴

只需将您的ajax代码放置在我用简单的html分隔符扩展内容的地方