Skip to content

Instantly share code, notes, and snippets.

@Juhlinus
Created February 13, 2020 09:46
Show Gist options
  • Save Juhlinus/cd9a0bde82705767c9e9cdf3be176e1c to your computer and use it in GitHub Desktop.
Save Juhlinus/cd9a0bde82705767c9e9cdf3be176e1c to your computer and use it in GitHub Desktop.
Infinite Scroll in InertiaJS + Vue
<template>
<div>
<ul>
<li v-for="product in products_data" class="py-6">
<inertia-link :href="'products/' + product.id">{{ product.name }}</inertia-link>
</li>
</ul>
</div>
</template>
<script>
import Layout from '@/Shared/Layout'
export default {
layout: Layout,
data() {
return {
products_data: this.products.data,
}
},
props: {
products: Object,
},
methods: {
scroll: function () {
const self = this;
window.onscroll = function(ev) {
if ((window.innerHeight + window.scrollY) >= document.body.scrollHeight) {
const product_links = self.products.links;
const next = product_links[product_links.length - 1];
if (next.url) {
self.$inertia.visit(next.url, {
preserveScroll: true,
preserveState: true,
});
self.products_data.push(...self.products.data);
}
}
};
}
},
mounted () {
this.scroll();
}
}
</script>
@crossfadetv
Copy link

The problem on that solution is that the first page disappears when the 2nd page is loaded. So it's not possible to scroll back or refresh the page. I've seen some solutions using axios in the www.

@MMTE
Copy link

MMTE commented Jun 24, 2022

infinity scroll in inertia js

this link is also useful. it has inertia URL change problem solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment