大家好,我的无尽页面 js 代码运行良好,但由于某种原因,它只触发第二页。我对找到的一些代码进行了一些更改以满足我的需求,但现在不是继续加载项目,而是在我的控制台记录状态时在第 2 页停止。这是我的代码,有人可以检查它并告诉我为什么这没有触发吗?至于我的分页,我使用的是 will_paginate gem,我得到了底部页面的链接,所以后面的 es 工作正常。先感谢您。
编辑 2:如果有人遇到我的问题,这是我的最终设置。
- content_for :scripts do
:javascript
(function() {
var divContainer = $('#posts-container');
var page = 1,
loading = false;
var current_url = divContainer.data('url') + '?page=';
function nearBottomOfPage() {
return $(window).scrollTop() >= $(document).height() - $(window).height() - 10;
}
$(window).scroll(function(){
if (loading) {
return;
}
if(nearBottomOfPage()) {
loading=true;
page++;
$.ajax({
url: current_url + page,
type: 'get',
dataType: 'script',
success: function(html) {
var $items = $(html).find('.item');
jQuery(".content").append($items).masonry( 'appended', $items, true );
$(".content").masonry('reload');
},
error: function() {
console.log("Error")
},
complete: function() {
loading = false;
}
});
}
});
}());
编辑1:
我发现 nearBottomOfPage() 函数无法正常工作。它只触发第一次,并且永远不会再次返回 true。为什么会这样?