-
-
Save shankardevy/dcb57a3bf64303bab1ac6dde94670170 to your computer and use it in GitHub Desktop.
Hook for trigger event on browser scroll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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