Created
June 4, 2020 07:55
-
-
Save RuslanHolovko/ff2ee16fa5059438c8feb7057dae274e to your computer and use it in GitHub Desktop.
Observer component for vue js
This file contains hidden or 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
<!-- Observer.vue --> | |
<template> | |
<div class="observer"/> | |
</template> | |
<script> | |
export default { | |
data: () => ({ | |
observer: null, | |
}), | |
mounted() { | |
this.observer = new IntersectionObserver(([entry]) => { | |
if (entry && entry.isIntersecting) { | |
this.$emit("intersect"); | |
} | |
}); | |
this.observer.observe(this.$el); | |
}, | |
destroyed() { | |
this.observer.disconnect(); | |
}, | |
}; | |
</script> | |
How to use in component | |
<!-- intersectionobserver --> | |
<observer @intersect="loadPosts" v-if="!showSearch"/> | |
<!-- intersectionobserver --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment