DocumentationLoading

Loading

Show a big gallery in parts with a Load more button, infinite scroll or numbered pagination. Set how many items appear first and how many each step adds.

By default a grid shows its first 12 items and a Load more button for the rest. The load option changes that. It has three settings:

KeyDefaultMeaning
mode'button''all', 'button', 'scroll' or 'pages'.
initial12How many items show first (and the page size in pages mode).
step8How many items each load adds, in button and scroll modes.

As attributes these are load, initial and step:

html
<media-boxes load="button" initial="12" step="6"> ... </media-boxes>
js
grid.options = { load: { mode: 'scroll', initial: 12, step: 8 } };

All items are in the page from the start. Items that are not shown yet stay hidden and their images do not load (they are loading="lazy" and not displayed), so a long gallery stays light. Search engines still see all of the markup. The loading modes demo lets you switch between the four modes on one grid.

All at once #

mode: 'all' shows every item and draws no footer. Use it for short galleries.

Load more button #

mode: 'button' shows a button under the grid. Its label is i18n.loadMore followed by the number of items still hidden, for example "Load more 24". The button disappears when everything is shown.

html
<media-boxes load="button" initial="9" step="9" columns="3"> ... </media-boxes>

Translate the label with i18n: { loadMore: 'Show more' }. You can also trigger the same action from a button anywhere in the page:

html
<button type="button" data-mb-for="work" data-mb-load-more>Show more work</button>

When the keyboard focus is on the Load more button and the last items load, focus moves to the first new item so it is not lost.

Infinite scroll #

mode: 'scroll' adds the next step when the end of the grid comes within about 600px of the viewport. There is no button. It uses an IntersectionObserver on a hidden marker under the grid.

html
<media-boxes load="scroll" initial="12" step="8"> ... </media-boxes>

Pagination #

mode: 'pages' splits the items into pages of initial items and draws numbered buttons with previous and next arrows. When there are more than seven pages the list collapses with an ellipsis. step is not used in this mode.

html
<media-boxes load="pages" initial="8" columns="4"> ... </media-boxes>

The page buttons are real buttons: the current one has aria-current="page" and each has a label such as "Page 3" from i18n.page. Previous and next use i18n.previous and i18n.next. The navigation landmark is called "Pagination", which you can change with i18n.pagination. Changing page scrolls the top of the grid into view if it was above the window.

Filtering, searching and sorting always start again from the first chunk (or the first page). The counts and the number of pages follow the filtered set. In button and scroll modes the lightbox lets people flip through filtered items that are not displayed yet, unless you set lightbox.scope to 'all' (see Lightbox).

API and event #

js
const grid = document.getElementById('work');

grid.loadMore();   // button and scroll modes: add one step
grid.goToPage(3);  // pages mode: go to page 3 (clamped to the available pages)

grid.addEventListener('mb:load', (e) => {
  const { added, visible, total } = e.detail;
  console.log(`${added} new, ${visible} of ${total} shown`);
});

mb:load fires after loadMore() and goToPage(), with the number of items just added, how many are visible and how many pass the filters in total. Both methods do nothing in a mode where they do not apply.