Skip to content

Instantly share code, notes, and snippets.

@james0r
Created April 13, 2020 16:15
Show Gist options
  • Save james0r/cd0a3dc7bf3f772186eb2f041bbb0c04 to your computer and use it in GitHub Desktop.
Save james0r/cd0a3dc7bf3f772186eb2f041bbb0c04 to your computer and use it in GitHub Desktop.
infinite scroll code
var current_page = 1;
var awaiting = false;
function infinite_is_in_view() {
var scrollTop = $(window).scrollTop(),
elementOffset = $('.infinite-more-link.blog').offset().top,
distance = (elementOffset - scrollTop);
if (distance < $(window).height()) {
if (!awaiting) {
awaiting = true;
$.ajax({
url: '/posts-endpoint/' + current_page + '/?posttype=blog',
type: 'GET',
dataType: 'json', // added data type
success: function(res) {
res.forEach(function(element) {
$('.blog-articles').append(render_grid_card(element));
$('article').fadeIn();
})
awaiting = false;
current_page++;
},
error: function (xhr,status,error) {
console.log("We've encountered the following error: " + error)
}
});
}
} else {
return false;
}
}
$(window).on("scroll load", function(event) {
infinite_is_in_view();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment