I use Kaminari for pagination. And here is the code that I use for infinite scrolling:
jQuery(function() {
if ($('.pagination').length) {
$(window).scroll(function() {
var url;
url = $('.pagination .next_page').attr('href');
if(url && $(window).scrollTop()>$(document).height()-$(window).height()-50)
{
$('.pagination').text("Fetching more products...");
return $.getScript(url);
}
});
return $(window).scroll();
}
The above code I wrote in the document.ready function. And then this code is written in the .js.erb file
$("#container1").append("<%= escape_javascript(render 'shirts/first')%>");
<% if @first.next_page %>
$('.pagination').replaceWith("<%= j paginate @first %>");
<% else %>
$('.pagination').remove();
<% end %>
<% sleep 1 %>
});
But I do not get an endless scroll. Can someone direct me to where I am wrong?
source
share