Infinite Scroll
Implement infinite scroll
IntermediateTopic: Mini Projects
JavaScript Infinite Scroll Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
class InfiniteScroll {
constructor(loadMore) { this.loadMore = loadMore; this.page = 1; }
init() { window.addEventListener('scroll', () => { if(window.innerHeight + window.scrollY >= document.body.offsetHeight) { this.loadMore(this.page++); } }); }
}Output
// Infinite scroll
Understanding Infinite Scroll
Infinite Scroll loads content as user scrolls.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.