npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-intersection

v2.1.0

Published

Vue component for observing scrolling of long lists of child components

Downloads

70

Readme

vue-intersection

Summary

vue-intersection is a library that implements an IntersectionObserver, which can be extremely helpful in a variety of scenarios. Probably the most common use case is for lazy loading images in a long gallery or for infinitely loading more content whenever the user scrolls to the bottom of your page, but it can be used to improve performance of scrollable UIs in any number of ways. You will find some other examples below. Whenever you want to offload an action until a particular element in scrolled into the user's view, this is a great tool to help you do that with a very simple API.

Installation

yarn add vue-intersection

or

npm i --save vue-intersection

Basic Usage

Simply wrap your container in an IntersectionRoot component and then wrap each child element that you want to observe in a IntersectionChild component. The the Child components do not have to be direct decendents of the Root. You can have whatever content you need in between them. And not every child has to be observed. You can instead choose to observe only a few children if that is your use case. Then you can listen to whenever the child enters/leaves the container's viewport by listening to the @enter or @leave events.

<template>
    <IntersectionRoot :threshold="threshold" :rootMargin="rootMargin">
      <IntersectionChild
        v-for="child in children"
        :key="child.id"
        @enter="child.visible = true"
        @leave="child.visible = false"
      >
        {{ child.id }} is {{ child.visible ? '' : 'not' }} visible
      </IntersectionChild>
    </IntersectionRoot>
  </div>
</template>
<script>
import { IntersectionRoot, IntersectionChild } from 'vue-intersection'

export default {
    name: "MyComponent",
    components: { IntersectionRoot, IntersectionChild },
    data() {
        return {
            threshold: 1,
            rootMargin: "0px",
            children: Array.from(Array(50).keys())
                .map(id => ({ id, visible: false }))
            };
        };
    }
}
</script>

API Details

IntersectionRoot

| Prop | Type | Default | Description | |---|---|---|---| | threshold | Array | [0,1] | Corresponds to the threshold option on the IntersectionObserver. Note: if you do not include 0 as a threshold value, the @leave event will never be fired. | | rootMargin | Number/String | "0px" | Corresponds to the rootMargin option on the IntersectionObserver | | startThreshold | Number/String | "0px" | Distance (px) from the top of the container for when to fire the @start event | endThreshold | Number/String | "0px" | Distance (px) from the bottom of the container for when to fire the @end event | debounce | Number | 10 | Time (ms) to debounce scroll events (only affects @start, @end, @middle, not @enter, @leave).

| Event | Type | Arguments | Optional | | --- | --- | --- | --- | | @start | Function | scroll event | true | | @end | Function | scroll event | true | | @middle | Function | scroll event | true | NOTE: These 3 events are debounced and will fire (at most) once every 10ms, by default. If this is too frequent, set the debounce prop to a higher delay, which will improve peformance. To capture all events, set debounce to 0.

IntersectionChild

| Prop | Type | Default | Description | |---|---|---|---| | id | String | Randomly generated | If a IntersectionChild has an id property, it will be used as the unique identifier for scroll tracking. It is recommended to provide an id as it improves performance slightly.

| Event | Type | Arguments | Description | |---|---|---|---| | @enter | Function | IntersectionObserverEntry | Will be fired whenever the child element enters the viewport of the IntersectionRoot container | | @leave | Function | IntersectionObserverEntry | Will be fired whenever the child element leaves the viewport of the IntersectionRoot container |

Demo

You can find additional examples/demos below:

Browser Support

vue-intersection works in all browsers that support the IntersectionObserver API, which is essentially every major browser except for IE 11. If you need support for IE 11, you can use any of the IntersectionObserver polyfills that are available. For a list of supported browsers, look here.

Alternative Libraries

Justification

  • The main reason I added another library on top of these other options is because each of these above is creates a new IntersectionObserver instance for each element that is being observed. So if you're trying to observe a large number of elements in a scrollable list, then that is a lot of observer instances. However, vue-intersection was designed such that there is only one observer instance created for each IntersectionRoot component, which observes each of the nested IntersectionChild elements. Thus, the performance is much better for long lists of elements.
  • Another shortcoming that several of these other libraries have is that they don't provide enough granular support on which container and which target elements to observe. Several of the above only support the root element being the document.body element, but often it is helpful to observe scrolling within some other element on your page. vue-intersection allows for much more fine-grain control and flexibility while still maintaining a simple API.