-
-
Save james0r/cd0a3dc7bf3f772186eb2f041bbb0c04 to your computer and use it in GitHub Desktop.
infinite scroll code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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