Skip to main content

How to Create Infinite Scroll in CSS and JavaScript

Learn to implement infinite scroll using the Intersection Observer API. Create seamless, endless scrolling experiences like social media feeds.

Live Demo

Scroll down to load more

Item 1

This is content item number 1. Scroll down to load more items automatically.

Item 2

This is content item number 2. Scroll down to load more items automatically.

Item 3

This is content item number 3. Scroll down to load more items automatically.

Item 4

This is content item number 4. Scroll down to load more items automatically.

Item 5

This is content item number 5. Scroll down to load more items automatically.

How It Works

  • 1.An Intersection Observer watches a "sentinel" element at the bottom of the list
  • 2.When the sentinel becomes visible (user scrolled near bottom), it triggers a callback
  • 3.The callback fetches more data from an API and appends new items
  • 4.Loading states prevent duplicate requests; hasMore tracks when content is exhausted

Why Intersection Observer? It's asynchronous and doesn't block the main thread, unlike scroll event listeners which fire on every scroll and can cause jank.

Step-by-Step Tutorial

Follow these steps to implement infinite scroll. Click any code block to copy.

1

Set Up HTML Structure

Create a container for your items, a sentinel element that the observer will watch, and loading/end indicators.

2

Add CSS Styles

Style your container, items, loading spinner, and end message. The spinner uses a CSS animation for the rotating effect.

3

Initialize Intersection Observer

Create an Intersection Observer that watches the sentinel element. The rootMargin option starts loading before the sentinel is fully visible.

4

Load More Content

When the observer triggers, fetch new content from your API and append it to the container. Use an isLoading flag to prevent duplicate requests.

5

Handle End of Content

When there's no more content, disconnect the observer and show an end message. This prevents unnecessary API calls.

Complete Reusable Implementation

Here's a complete, reusable class-based implementation:

Best Practices

Do

  • +Use rootMargin to preload content before user reaches bottom
  • +Implement loading states to prevent duplicate requests
  • +Disconnect observer when content is exhausted
  • +Provide "Load More" button as accessibility fallback
  • +Show skeleton loaders during loading

Don't

  • -Use scroll event listeners without throttling
  • -Load too much data at once (keep to 10-20 items)
  • -Forget to clean up observers on unmount
  • -Use for SEO-critical paginated content
  • -Ignore error handling for failed requests

Browser Support

Intersection Observer has excellent support in modern browsers:

Chrome

51+

Firefox

55+

Safari

12.1+

Edge

15+

IE

No support

For legacy browser support, use a polyfill or fall back to scroll event listeners.

Frequently Asked Questions

What is infinite scroll?

Infinite scroll is a web design technique that automatically loads more content as the user scrolls down the page. Instead of traditional pagination with page numbers, new content appears seamlessly, creating an endless scrolling experience like social media feeds.

Should I use Intersection Observer or scroll event listener?

Use Intersection Observer API for better performance. It runs asynchronously and doesn't block the main thread. Scroll event listeners fire on every scroll, which can cause jank. Only use scroll events if you need to support IE11 or older browsers.

How do I prevent too many API requests with infinite scroll?

Use a loading flag (isLoading) to prevent duplicate requests while one is in progress. Also implement a hasMore flag to stop requests when all content is loaded. Set rootMargin on your Intersection Observer to preload content before the user reaches the bottom.

Is infinite scroll good for SEO?

Infinite scroll can hurt SEO because search engines may not trigger the scroll events needed to load more content. For SEO-critical pages, consider using pagination or implement progressive enhancement with a 'Load More' button as fallback.

When should I NOT use infinite scroll?

Avoid infinite scroll for e-commerce category pages (users need to compare and bookmark items), pages with important footer content, content with a logical endpoint (like chapters), and when users need to return to a specific position.