vue-scrollto

repository·main·Indexed 24 days ago

https://github.com/rigor789/vue-scrollto

A lightweight Vue.js library for performing smooth scrolling animations to specific elements or containers. It supports Vue 2, Vue 3, and Nuxt.js, providing both a `v-scroll-to` directive and a programmatic `$scrollTo` method. Key features include customizable duration, easing (via bezier-easing), offsets, and lifecycle callbacks such as `onStart`, `onDone`, and `onCancel`.

Tokens
3.9K
Snippets
11
Records
29
Agent score
80%

What's inside vue-scrollto

  1. Use vue-scrollto as a Vue directive

    main

    Register the plugin using Vue.use(VueScrollTo). You can then use the v-scroll-to directive on any element. You can pass a selector string or a full options object to customize the behavior of specific scrolls.

    // Setup
    var Vue = require('vue');
    var VueScrollTo = require('vue-scrollto');
    Vue.use(VueScrollTo);
    
    // Usage in template
    // Simple selector
    <a href="#" v-scroll-to="'#element'">Scroll to #element</a>
    
    // With custom options
    <a href="#" v-scroll-to="{
         el: '#element',
         container: '#container',
         duration: 500,
         lazy: false,
         easing: 'linear',
         offset: -200,
         force: true,
         cancelable: true,
         onStart: onStart,
         onDone: onDone,
         onCancel: onCancel,
         x: false,
         y: true
    }">
        Scroll to #element
    </a>
  2. Install vue-scrollto

    main

    You can install vue-scrollto using npm, yarn, or by including it directly in your HTML via CDN. Note that the package was renamed from vue-scrollTo to vue-scrollto.

    npm install --save vue-scrollto
    # or
    yarn add vue-scrollto
  3. Configure vue-scrollto in Nuxt.js

    main

    To use vue-scrollto in a Nuxt.js project, add vue-scrollto/nuxt to the modules section of your nuxt.config.js. You can optionally pass configuration options as a second element in the module array.

    {
        modules: [
            'vue-scrollto/nuxt',
    
            // Or if you have custom options...
            ['vue-scrollto/nuxt', { duration: 300 }],
        ]
    }
  4. Use the lazy option to recalculate targets

    main
    In version 2.20.0 and later, you can use the lazy option. This allows the library to recalculate the target element's position at the time of scrolling, which is useful if the DOM structure or element positions change between the time the directive is applied and when the scroll is triggered.