Skip to content

Instantly share code, notes, and snippets.

@shankardevy
Created April 19, 2023 06:37
Show Gist options
  • Save shankardevy/dcb57a3bf64303bab1ac6dde94670170 to your computer and use it in GitHub Desktop.
Save shankardevy/dcb57a3bf64303bab1ac6dde94670170 to your computer and use it in GitHub Desktop.
Hook for trigger event on browser scroll
// assets/js/infinite_scroll.js
export default InfiniteScroll = {
page() {return this.el.dataset.page;},
loadMore(entries) {
const target = entries[0];
if (target.isIntersecting && this.pending == this.page()) {
this.pending = this.page() + 1;
this.pushEvent("load-more", {});
}
},
mounted() {
this.pending = this.page();
this.observer = new IntersectionObserver(
(entries) => this.loadMore(entries),
{
root: null,
rootMargin: "400px",
threshold: 0.1,
}
);
this.observer.observe(this.el);
},
destroyed() {
this.observer.unobserve(this.el);
},
updated() {
this.pending = this.page();
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment