Moey extras
Infinite scroll
The next items load as you reach the end of the list — feeds, galleries, search results. After a few loads it waits for a "Load more" button, so people can still get to your footer; it says when it's loading and when there's nothing more.
<div class="masonry" id="feed" data-moey-masonry data-moey-infinite data-moey-enter>
…the first items…
</div>
<script>
let page = 1;
feed.addEventListener('moey:load', (e) => {
e.detail.waitUntil(fetch('/api/photos?page=' + ++page).then((r) => r.json()).then((items) => {
feed.insertAdjacentHTML('beforeend', items.map(toTileHTML).join(''));
if (items.length < 20) e.detail.done(); // that was the last page
}));
});
</script>
Step by step
- Add
data-moey-infiniteto the list or grid. Moey puts a small status line and an invisible marker after it. - Load in
moey:load: fetch the next page, append the items, and hand the promise toe.detail.waitUntil()so the spinner shows while it loads. - Say when it's over: call
e.detail.done()on the last page — "That's everything" appears (data-moey-infinite-endto change the words). - Choose how far it goes alone: after
data-moey-infinite-autoloads (3) a "Load more" button takes over;data-moey-infinite="button"always uses the button.
Good to know
- It loads a little before the end (400px early), so scrolling rarely has to wait.
- It works with masonry, filters, list groups and cards; new items join the layout and the current filter.
- Add
data-moey-enterand the new items fade up into place, one after another (more). - Screen readers hear "Loading…", "8 more loaded" and "That's everything".
- Keep links to items working: give the items real URLs so people can come back to them.
Reference
| Attribute / API | What it does |
|---|---|
data-moey-infinite · ="button" | Loads more near the end · only with the button |
data-moey-infinite-auto="3" · data-moey-infinite-end | Loads before the button takes over · the words at the end |
Event moey:load | detail: { page, waitUntil(promise), done() } |
Moey.infinite(el) | { load(), reset() } (reset after a new search) |