Infinite Scroll
Load more items as the user scrolls to the bottom of a list.
Live Demo
Loading more...
Source
HTML + JavaScript
<div data-component="infinite-scroll">
<div class="scroll-container" data-action="scroll:checkScroll">
<div data-list="items">
<template>
<div class="scroll-item" data-bind="label"></div>
</template>
</div>
<div data-show="loadingMore">Loading more...</div>
</div>
</div>
<script>
wildflower.component('infinite-scroll', {
state: { items: [], loadingMore: false, page: 0, hasMore: true },
computed: {
statusText() {
return this.items.length + ' items loaded'
+ (this.hasMore ? ', scroll for more' : ', all loaded');
}
},
init() { this._loadPage(); },
checkScroll(e) {
const el = e.target;
const nearBottom = el.scrollHeight - el.scrollTop - el.clientHeight < 50;
if (nearBottom && !this.loadingMore && this.hasMore) this._loadPage();
},
_loadPage() {
this.loadingMore = true;
setTimeout(() => {
const start = this.page * 20;
for (let i = 0; i < 20; i++) {
this.items.push({ label: 'Item ' + (start + i + 1) });
}
this.page++;
this.loadingMore = false;
if (this.page >= 5) this.hasMore = false;
}, 600);
}
});
</script>
Key Points
data-action="scroll:checkScroll"binds the scroll event on the container, checking proximity to bottom- A
loadingMoreguard prevents duplicate fetches while a page is loading this.items.push()appends items. WF's reactive proxy handles array mutationshasMorestate disables further loading once all data is fetched