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="griddy" id="feed" data-moey-griddy 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, so people can still reach your footer. A click makes it automatic again for the same number of loads.data-moey-infinite-auto="all"never stops;data-moey-infinite="button"always uses the button.
A button first, then automatic
data-moey-infinite="once" starts with the "Load more" button, so nothing loads until someone asks. After the first click it loads by itself as they scroll (up to data-moey-infinite-auto loads, then the button again). While it loads, the button stays where it is, with a spinner and "Loading…" inside it, so nothing on the page jumps. Try it:
<div class="griddy" id="feed" data-moey-griddy data-moey-infinite="once" data-moey-enter="pop">
…the first items…
</div>
Good to know
- It loads a little before the end (400px early), so scrolling rarely has to wait.
- It works with griddy, 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". When the button goes away after a click, keyboard focus moves to the first new item.
- 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" · ="once" | Loads more near the end · only with the button · the button first, then automatic |
data-moey-infinite-auto="3" · ="all" | Automatic loads before the button takes over · never stop |
data-moey-infinite-more · data-moey-infinite-end | The button's words ("Load more") · the words at the end |
Event moey:load | detail: { page, waitUntil(promise), done() } |
Moey.infinite(el) | { load(), reset() } (reset after a new search) |