Skip to content

Instantly share code, notes, and snippets.

@romainavalle
Last active June 10, 2022 09:54
Show Gist options
  • Save romainavalle/a76b6340577ffd7e9fba0c48db116dcb to your computer and use it in GitHub Desktop.
Save romainavalle/a76b6340577ffd7e9fba0c48db116dcb to your computer and use it in GitHub Desktop.
observer mixin for nuxtjs
export default {
data() {
return { isShown: false, isActive: false, boundingClientRect: {} }
},
methods: {
intersectionCb(entries) {
if (entries[0].intersectionRatio > 0) {
this.isShown = true
this.isActive = true
this.boundingClientRect = entries[0].boundingClientRect
}
if (entries[0].intersectionRatio <= 0) {
this.isActive = false
}
},
},
beforeDestroy() {
if (this.observer) this.observer.unobserve(this.$el)
},
mounted() {
this.observer = new IntersectionObserver(this.intersectionCb, {})
this.observer.observe(this.$el)
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment