vue3-track
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

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.

Package Sidebar

Install

npm i vue3-track

Weekly Downloads

8

Version

1.1.1

License

MIT

Unpacked Size

18.8 kB

Total Files

12

Last publish

Collaborators

  • dschewchenko