Last active
December 28, 2020 11:47
-
-
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)
This file contains 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
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