Last active
June 10, 2022 09:54
-
-
Save romainavalle/a76b6340577ffd7e9fba0c48db116dcb to your computer and use it in GitHub Desktop.
observer mixin for nuxtjs
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
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