Vue Intersect (Vue 2 Component)

1.1.6 · abandoned · verified Sun Apr 19

Vue Intersect is a Vue 2 component designed to wrap the native Intersection Observer API, providing a declarative way to detect when an HTML element enters or leaves the viewport. Last published in March 2020 (version 1.1.6), this package is specifically compatible with Vue 2.x projects and has not been updated for Vue 3. Modern Vue 3 applications typically implement similar functionality using composables (e.g., `@intersection-observer/vue`) or directives, which align better with the Composition API. Due to its sole reliance on Vue 2 and lack of recent development, it is considered abandoned for new projects, though it remains functional for existing Vue 2 applications. Its primary differentiation was simplifying a browser API within the Vue 2 ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates global registration of the `Intersect` component and its usage with `change`, `enter`, and `leave` events. It shows how to track visibility and trigger actions when an element scrolls into or out of view, with a custom threshold.

import Vue from 'vue';
import Intersect from 'vue-intersect';

Vue.component('Intersect', Intersect);

new Vue({
  el: '#app',
  data: {
    isVisible: false
  },
  methods: {
    handleIntersect(entry) {
      this.isVisible = entry.isIntersecting;
      console.log('Intersection changed:', entry.isIntersecting);
    },
    handleEnter(entry) {
      console.log('Element entered viewport!', entry.target);
    },
    handleLeave(entry) {
      console.log('Element left viewport!', entry.target);
    }
  },
  template: `
    <div id="app" style="height: 1200px; padding-top: 500px;">
      <h1>Scroll down to see the intersection</h1>
      <p>Visibility status: {{ isVisible ? 'Visible' : 'Hidden' }}</p>
      <div style="height: 500px; background: #eee; margin-top: 200px;"></div>
      
      <Intersect @change="handleIntersect" @enter="handleEnter" @leave="handleLeave" :threshold="[0, 0.5, 1]">
        <div style="width: 300px; height: 200px; background-color: lightblue; border: 2px solid blue;">
          This box is observed for intersection.
        </div>
      </Intersect>

      <div style="height: 800px; background: #eee; margin-top: 200px;"></div>
    </div>
  `
});

view raw JSON →