Scroll-driven content loading with AJAX pagination
Scroll down inside the container to trigger loading of new items. This demo uses a mock data source instead of a real AJAX endpoint.
Configuration object passed to new Sadrazam.InfiniteScroll({ ... })
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
| scrollElement | Element | Window | Yes | null | The element whose scroll event is listened to. Can be window or any scrollable DOM element (e.g. a <div> with overflow). |
| listElement | Element | Yes | null | The container element where new items are appended (e.g. a <ul> or <div>). |
| source | String | Yes | null | The AJAX endpoint URL that returns paginated JSON data. |
| startPage | Number | No | 1 | The initial page number to start fetching from. |
| setInnerItem | Function | Yes | () => {} | A callback receiving each data item; must return a DOM element (e.g. <li>) to be appended to the list. |
The AJAX endpoint must return JSON in this shape. The component sends { page: N } as request data.
// GET /api/items?page=2 { "items": [ // Array of data objects { "id": 21, "title": "Item 21" }, { "id": 22, "title": "Item 22" }, ... ], "last": 40, // Index/count of the last item returned "limit": 100 // Total number of items (0 = unlimited) }
Minimal setup to get infinite scroll working with a scrollable container
const scroller = new Sadrazam.InfiniteScroll({ scrollElement: document.getElementById('myScrollContainer'), listElement: document.getElementById('myList'), source: '/api/products', startPage: 1, setInnerItem: (item) => { const li = document.createElement('li'); li.textContent = item.title; return li; } });
Use window as the scroll element for full-page infinite scrolling
const pageScroller = new Sadrazam.InfiniteScroll({ scrollElement: window, // Listen on the window object listElement: document.querySelector('.feed'), source: '/api/feed', setInnerItem: (post) => { const article = document.createElement('article'); article.innerHTML = `<h3>${post.title}</h3><p>${post.body}</p>`; return article; } });
Instance methods available after construction
Class-level settings that affect all instances
How the component works under the hood
A complete pattern showing initialization, pause/resume controls, and teardown
const container = document.getElementById('feed-wrapper'); const list = document.getElementById('feed-list'); // Initialize const feed = new Sadrazam.InfiniteScroll({ scrollElement: container, listElement: list, source: '/api/posts', startPage: 1, setInnerItem: (post) => { const li = document.createElement('li'); li.className = 'post-item'; li.innerHTML = ` <strong>${post.title}</strong> <p>${post.excerpt}</p> `; return li; } }); // Pause when user opens a modal modal.on('open', () => feed.pause()); modal.on('close', () => feed.resume()); // Full teardown on page navigation function cleanup() { feed.destroy(); // list.__infiniteScroll is now null } // Access instance from the DOM element const ref = list.__infiniteScroll; // same as `feed` ref.reCalculate();