lugen4ro/nuxt3-intersection-observer-example

Intersection Observer API example for Nuxt3. Adds the custom directive v-instersect to execute callbacks when a component enters or leaves the viewport. Supports setting time and area thresholds.

★ 0Forks 0TypeScriptGitHub ↗Compare

README

Intersection Observer Example for Nuxt3

This is an implementation of a custom directive for Nuxt3 that utilizes the intersection observer API. The custom directive "v-intersect" allows you to execute custom callback functions when a component enter or leaves the viewport. In addition, you can customize this behaviour by setting a view time threshold and an area threshold to configure how long a component has to be in the viewport / how much area has to overlap with the viewport.

Files of concern are

  • app.vue --> Main page where you can see how the custom v-intersetct directive is used
  • plugins/intersectionDirective.ts --> Implemenation of the custom directive

The custom directive "v-intsersect" can be used like this.

<template>
    <div v-intersect="intersectOptions" v-for="index in 20" :key="index" class="panel">
        <p>{{ index }}</p>
    </div>
</template>

<script setup>
  const intersectOptions = {
      onBegin: (element) => { element.style.backgroundColor = "#f58142" }, // callback to execute when view time and area threshold are surpassed
      onEnd: (element) => { element.style.backgroundColor = "#02bfd4" }, // callback to execute when element leaves viewport
      totalViewTimeThresholdms: 1000, // must intersect 1000ms with the viewport 
      observerOptions: { threshold: 0.5 }, // must intsersect at least 50% with the viewport 
  }
</script>

Showcase

time threshold = 0 time threshold = 1s
Kapture 2024-08-01 at 22 15 55 Kapture 2024-08-01 at 22 32 11

How to use

Clone this repo and execute the following to install packages and start the dev server

yarn install # install dependences
yarn dev # start dev server

Then access http://localhost:3000

Contributors

lugen4ro

Issues