Intersection.vue 799 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <template>
  2. <div ref="container">
  3. <slot></slot>
  4. </div>
  5. </template>
  6. <script lang="ts">
  7. /*
  8. Implements a wrapper listening to viewport intersections via
  9. IntersectionObserver API
  10. Events
  11. ------
  12. intersecting (entry: IntersectionObserverEntry) -> When the component is intersecting the viewport
  13. */
  14. import { defineComponent } from "@nuxtjs/composition-api"
  15. export default defineComponent({
  16. data() {
  17. return {
  18. observer: null as IntersectionObserver | null,
  19. }
  20. },
  21. mounted() {
  22. this.observer = new IntersectionObserver(([entry]) => {
  23. if (entry && entry.isIntersecting) {
  24. this.$emit("intersecting", entry)
  25. }
  26. })
  27. this.observer.observe(this.$refs.container as Element)
  28. },
  29. beforeDestroy() {
  30. this.observer?.disconnect()
  31. },
  32. })
  33. </script>