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

vue3-track

v1.1.1

Published

A Vue 3 directive to track an element's position relative to the viewport on scroll and visibility.

Downloads

3

Readme

Vue3 Track

npm version npm downloads

Vue3 Track is a Vue.js directive and composable function that allows you to track the scroll position of an element relative to the viewport or a scroll container. It provides a simple and flexible way to create scroll-based effects and animations. It provides the following features:

  • Tracks the vertical and horizontal position
  • Tracks the vertical and horizontal visibility (with an optional offset)
  • Supports custom scroll containers.
  • Supports callback functions.
  • Provides CSS variables for styling.

Installation

You can install Vue3 Track using npm or yarn:

npm install vue3-track

or

yarn add vue3-track

Usage

Composable Function

To use Vue3 Track as a composable function, you need to import it and call it in the setup function of your component:

import { useVueTrack } from 'vue3-track';

export default {
  setup() {
    const element = ref(null); // reference to element that should be tracked
    const { position, visibility } = useVueTrack(element, { // same options as in directive
      selector: '.scroll-container',
      offset: 100,
      callback: (position, visibility) => {
        // ...
      }
    });

    return {
      position,
      visibility
    };
  }
};

The useVueTrack function returns the following reactive properties:

  • position: The vertical and horizontal position of the element relative to the scroll container or window.
  • visibility: The vertical and horizontal visibility of the element. It is true when the element is visible and false when it is not.
  • addListener: A function that adds a listener to the scroll container. For manual setup. Use only with 3rd argument false. By default, it is true and the listener is added automatically in the onMounted hook.
  • removeListener: A function that removes a listener from the scroll container. For manual setup. Use only with 3rd argument false. By default, it is true and the listener is removed automatically in the onUnmounted hook.

Directive

Global Registration

To use Vue3 Track globally in your project, you need to register it as a directive in your main entry file:

import { createApp } from 'vue';
import { VueTrackDirective, VueTrackPlugin } from 'vue3-track';

const app = createApp(App);

app.use(VueTrackPlugin);

app.mount('#app');

Local Registration

To use Vue3 Track locally in a specific component, you can import the directive and register it within the component:

import { VueTrackDirective } from 'vue3-track';

export default {
  directives: {
    trackScroll: VueTrackDirective
  },
  // ...
};

CSS Variables

The following CSS variables are available for styling:

  • --vue-track-y: The vertical position of the element relative to the scroll container or window.
  • --vue-track-x: The horizontal position of the element relative to the scroll container or window.
  • --vue-track-visible-y: The visibility of the element in the vertical direction. It is 1 when the element is visible and 0 when it is not.
  • --vue-track-visible-x: The visibility of the element in the horizontal direction. It is 1 when the element is visible and 0 when it is not.

You can use these CSS variables to apply different styles based on the scroll position and visibility of the tracked elements.

Options

The vue3-track directive supports the following options:

  • selector (optional): CSS selector for the scroll container. By default, it uses the window as the scroll container.
  • callback (optional): Callback function that is called when the element is scrolled. It receives the position and visibility of the element.
  • offset (optional): Offset for the vertical and horizontal visibility check. It adds a margin to the top and bottom (vertical) or left and right (horizontal) edges of the element before considering it visible. Only numbers are supported. By default, it is 0.

Note: The position is calculated from the top-left corner of the element on which the function or directive is applied.

Supported Browsers

Vue3 Track supports all modern browsers (>95% market share) without IE11 support.

Examples

To see some examples of using Vue3 Track, please refer to the src/App.vue file. And you can see the examples in action on the demo page.

Contributing

Contributions are welcome! If you encounter any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.

License

This project is licensed under the MIT License. See the LICENSE file for more details.