Skip to content

useIntersectionObserver

Detects when a target element enters or leaves the viewport. Useful for lazy loading, scroll-triggered animations, and "in view" detection.

Basic Usage

vue
<template>
  <div ref="target">
    <p v-if="isVisible">This content is visible!</p>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useIntersectionObserver } from "@baklavue/composables";

const target = ref(null);
const isVisible = useIntersectionObserver(target, {
  threshold: 0.5,
});
</script>

Lazy Loading

vue
<template>
  <div ref="target">
    <img v-if="isVisible" :src="imageSrc" alt="Lazy loaded" />
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useIntersectionObserver } from "@baklavue/composables";

const target = ref(null);
const isVisible = useIntersectionObserver(target, { threshold: 0 });
const imageSrc = ref("");
</script>

API

typescript
useIntersectionObserver(
  target: Ref<Element | null | undefined>,
  options?: UseIntersectionObserverOptions
): Ref<boolean>

Options

PropertyTypeDefaultDescription
rootElement | Document | nullnullRoot element (null = viewport)
rootMarginstring"0px"Margin around root
thresholdnumber | number[]0Visibility threshold 0-1
immediatebooleantrueStart observing immediately

Return Value

Returns a Ref<boolean> that is true when the target is visible (intersecting).