Skip to content

Instantly share code, notes, and snippets.

@bbscout
Last active December 28, 2020 11:47
Show Gist options
  • Save bbscout/b5a25d904603051949a1cf07ca9572e1 to your computer and use it in GitHub Desktop.
Save bbscout/b5a25d904603051949a1cf07ca9572e1 to your computer and use it in GitHub Desktop.
Infinite scroll - loading more content when reaching bottm of document (or offset from bottom)
window.onscroll = function() {
var offset = 100; //set offset from bottom
var loading = false;
var scrolledToOffset = document.documentElement.scrollTop + window.innerHeight + offset > document.documentElement.offsetHeight;
if ( scrolledToOffset && !loading ) {
function() {
loading = true; //block running function more than once before finishing previous request
//do something when reaching offset from bottom; e.g. load more content with AJAX
loading = false; //pass this in callback function when using async call
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment