Vue Smooth Scroll Directive

1.0.13 · active · verified Sun Apr 19

The `vue-smooth-scroll` package provides a lightweight, declarative smooth scrolling directive for Vue.js applications. It enables animated scrolling to anchor links within a page with customizable duration and offset. Currently at version 1.0.13, this library appears to be in a stable state with an infrequent release cadence, primarily targeting Vue 2 environments. Its key differentiators include a minimal footprint (under 1KB gzipped), straightforward integration through a global Vue directive, and a focus on simplicity rather than advanced features like scroll spy or complex easing functions. It serves as a practical solution for projects needing basic, reliable scroll-to-anchor functionality without the overhead of more extensive scrolling solutions. The project builds upon the foundations of the `vue-smoothscroll` library, aiming for ease of use and maintenance.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install, import, and globally register `vue-smooth-scroll` as a Vue plugin. It shows basic usage of the `v-smooth-scroll` directive with and without custom options, enabling smooth scrolling to anchor tags within a single-page application.

import { createApp } from 'vue'; // For illustration, assuming a Vue 2 project or compatibility layer
import vueSmoothScroll from 'vue-smooth-scroll';

// In a typical Vue 2 setup:
// import Vue from 'vue';
// Vue.use(vueSmoothScroll);

// For modern setups (e.g., Vue 3 with a compatibility layer or if plugin adapts):
const app = createApp({
  template: `
    <div id="app">
      <h1>Vue Smooth Scroll Example</h1>
      <nav>
        <a href="#section1" v-smooth-scroll>Go to Section 1</a>
        <a href="#section2" v-smooth-scroll="{ duration: 1000, offset: -70 }">Go to Section 2 (Offset)</a>
      </nav>
      <div style="height: 800px; background-color: #f0f0f0; margin-top: 20px;" id="section1">
        <h2>Section 1</h2>
        <p>Content for section 1.</p>
      </div>
      <div style="height: 800px; background-color: #e0e0e0; margin-top: 20px;" id="section2">
        <h2>Section 2</h2>
        <p>Content for section 2 with a custom offset and duration.</p>
      </div>
    </div>
  `,
});

// This line is crucial for registering the plugin in Vue 2.
// For Vue 3, a different registration might be needed if not fully compatible.
app.use(vueSmoothScroll);

app.mount('#app');

view raw JSON →