Infinite Scrolling
Load more content as the user scrolls.
Infinite Scrolling is a free JavaScript Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.
What Is Infinite Scrolling?
Infinite scrolling loads more content automatically as the user nears the bottom of a list, instead of requiring a Next Page click. Social feeds and search results commonly use it.
The Sentinel Pattern
The cleanest technique uses a sentinel: an empty element placed at the end of the list. When the sentinel scrolls into view, you fetch and append the next batch.
// Markup pattern:
// <ul id="feed"> ...items... </ul>
// <div id="sentinel"></div> <!-- trigger element -->Observing the Sentinel
Point an Intersection Observer at the sentinel. Because it sits after the last item, its visibility means the user has reached the end.
const sentinel = document.getElementById('sentinel');
const observer = new IntersectionObserver(onIntersect, {
rootMargin: '200px' // load a bit early
});
observer.observe(sentinel);Triggering a Load
In the callback, check isIntersecting. When true, load the next page of data.
function onIntersect(entries) {
entries.forEach(entry => {
if (entry.isIntersecting) {
loadMoreItems();
}
});
}Tracking the Page
Keep a page counter so each load fetches the next slice. Increment it after a successful fetch.
let page = 1;
async function loadMoreItems() {
const items = await fetchPage(page);
appendItems(items);
page++;
}Guarding Against Double Loads
The callback can fire repeatedly. Use a loading flag so a new fetch does not start while one is already in progress.
let loading = false;
async function loadMoreItems() {
if (loading) return;
loading = true;
const items = await fetchPage(page);
appendItems(items);
page++;
loading = false;
}Appending New Items
Add the fetched items before the sentinel so it stays at the bottom of the growing list, ready to trigger the next load.
function appendItems(items) {
const feed = document.getElementById('feed');
items.forEach(item => {
const li = document.createElement('li');
li.textContent = item.title;
feed.appendChild(li);
});
}Stopping at the End
When the server returns no more data, disconnect the observer so it stops trying to load. This prevents endless empty requests.
const items = await fetchPage(page);
if (items.length === 0) {
observer.disconnect(); // no more data
return;
}Why rootMargin Helps
A generous rootMargin (like 200px) starts loading before the user actually hits the bottom, so new items appear seamlessly without a visible pause.
new IntersectionObserver(onIntersect, {
rootMargin: '300px' // begin loading well before the end
});Accessibility Notes
Infinite scroll can trap keyboard and screen-reader users and hide the footer. Consider a manual Load More button as a fallback and announce new content politely with ARIA live regions.
The Complete Flow
Infinite scroll with Intersection Observer: place a sentinel, observe it, load and append data when it appears, guard against duplicate loads, and disconnect when the data runs out. No scroll math required.
Quick Check
Test your understanding of infinite scrolling.
Recap
You built infinite scrolling:
- Place a sentinel element after the list.
- Observe it with an Intersection Observer.
- On intersection, fetch and append the next page.
- Use a
loadingflag to avoid duplicate fetches. - Disconnect when data is exhausted.
Next, scroll-triggered animations.
Learn JavaScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 59
- Lessons
- 200
Frequently Asked Questions
Is the “Infinite Scrolling” lesson free?
Yes — any 3 lessons of the JavaScript Academy learning path, including “Infinite Scrolling”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.
What will I learn in “Infinite Scrolling”?
Load more content as the user scrolls. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Infinite Scrolling” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.