Convert Figma logo to code with AI

rigor789 logovue-scrollto

Adds a directive that listens for click events and scrolls to elements.

2,069
99
2,069
80

Top Related Projects

A lightweight script to animate scrolling to anchor links.

A lightweight JavaScript library for creating particles

Scroll Behavior polyfill

Scrollytelling with IntersectionObserver.

The javascript library for magical scroll interactions.

Quick Overview

Vue-scrollto is a lightweight Vue.js directive that provides smooth scrolling functionality to elements on a page. It allows developers to easily implement scrolling animations with customizable options, making it a versatile tool for improving user experience in Vue applications.

Pros

  • Easy to integrate with Vue.js projects
  • Highly customizable with various options for easing, duration, and offset
  • Supports both Vue 2 and Vue 3
  • Lightweight and has no dependencies

Cons

  • Limited to Vue.js applications only
  • May not be suitable for complex scrolling animations or parallax effects
  • Requires additional configuration for scrolling within custom containers
  • Documentation could be more comprehensive for advanced use cases

Code Examples

  1. Basic usage as a directive:
<template>
  <div>
    <button v-scroll-to="'#element'">Scroll to Element</button>
    <div id="element">Target Element</div>
  </div>
</template>
  1. Using the programmatic API:
import VueScrollTo from 'vue-scrollto'

// Scroll to element with ID 'element'
VueScrollTo.scrollTo('#element', 500, { easing: 'ease-in' })
  1. Custom options:
<template>
  <button v-scroll-to="{
    el: '#target',
    duration: 800,
    easing: 'ease-out',
    offset: -60
  }">
    Scroll with Custom Options
  </button>
</template>

Getting Started

  1. Install the package:

    npm install vue-scrollto
    
  2. Import and use in your Vue app:

    import Vue from 'vue'
    import VueScrollTo from 'vue-scrollto'
    
    Vue.use(VueScrollTo)
    
  3. Use in your components:

    <template>
      <button v-scroll-to="'#target'">Scroll to Target</button>
    </template>
    

Competitor Comparisons

A lightweight script to animate scrolling to anchor links.

Pros of smooth-scroll

  • Framework-agnostic, can be used with any JavaScript project
  • Supports both modern and older browsers, including IE9+
  • Offers more customization options, such as custom easing functions

Cons of smooth-scroll

  • Larger file size (about 3.5KB minified and gzipped)
  • Requires more setup and configuration for basic usage
  • Not specifically optimized for Vue.js projects

Code Comparison

smooth-scroll:

var scroll = new SmoothScroll('a[href*="#"]', {
    speed: 500,
    easing: 'easeInOutCubic'
});

vue-scrollto:

import VueScrollTo from 'vue-scrollto'
Vue.use(VueScrollTo)

// In component
this.$scrollTo('#element', 500, { easing: 'ease-in' })

Key Differences

  1. Integration: vue-scrollto is specifically designed for Vue.js, while smooth-scroll is framework-agnostic.
  2. API: vue-scrollto provides a more Vue-friendly API with directives and methods, whereas smooth-scroll uses a more traditional JavaScript approach.
  3. File size: vue-scrollto is smaller (about 1.5KB gzipped) compared to smooth-scroll.
  4. Browser support: smooth-scroll supports older browsers, while vue-scrollto focuses on modern browsers.
  5. Customization: smooth-scroll offers more advanced customization options, but vue-scrollto provides easier integration with Vue.js projects.

Choose smooth-scroll for non-Vue projects or when extensive customization is needed. Opt for vue-scrollto in Vue.js applications for easier integration and smaller file size.

A lightweight JavaScript library for creating particles

Pros of particles.js

  • Highly customizable particle animation system
  • Supports various shapes, colors, and interaction modes
  • Can create visually stunning backgrounds and effects

Cons of particles.js

  • Larger file size and potentially higher performance impact
  • May not be as easy to integrate into Vue.js projects
  • Focused on particle animations, not general-purpose scrolling

Code Comparison

particles.js:

particlesJS("particles-js", {
  particles: {
    number: { value: 80, density: { enable: true, value_area: 800 } },
    color: { value: "#ffffff" },
    shape: { type: "circle" }
  }
});

vue-scrollto:

import VueScrollTo from 'vue-scrollto'
Vue.use(VueScrollTo)

// In component
this.$scrollTo('#element', 500, { easing: 'ease' })

Summary

particles.js is a powerful library for creating particle animations, offering extensive customization options for creating visually appealing effects. However, it may have a higher performance impact and is less focused on Vue.js integration.

vue-scrollto, on the other hand, is specifically designed for Vue.js projects and provides smooth scrolling functionality with a smaller footprint. It's more suitable for projects that require simple scrolling behavior rather than complex particle animations.

The choice between these libraries depends on the specific needs of your project, whether you prioritize visual effects or smooth scrolling functionality in a Vue.js environment.

Scroll Behavior polyfill

Pros of smoothscroll

  • Framework-agnostic, can be used with any JavaScript project
  • Implements the CSSOM View Module's scroll behavior
  • Provides a polyfill for browsers that don't support smooth scrolling natively

Cons of smoothscroll

  • Less feature-rich compared to vue-scrollto
  • Not specifically optimized for Vue.js applications
  • May require more manual configuration for advanced use cases

Code Comparison

vue-scrollto:

import VueScrollTo from 'vue-scrollto'
Vue.use(VueScrollTo)

// In a component
this.$scrollTo('#element', 500, { easing: 'ease-in' })

smoothscroll:

import smoothscroll from 'smoothscroll-polyfill'
smoothscroll.polyfill()

// Usage
window.scroll({ top: 1000, left: 0, behavior: 'smooth' })

Key Differences

  1. Integration: vue-scrollto is specifically designed for Vue.js, while smoothscroll is a general-purpose solution.
  2. API: vue-scrollto provides a more declarative API, whereas smoothscroll enhances the native scroll behavior.
  3. Features: vue-scrollto offers more customization options out of the box, such as custom easing functions and offset calculations.
  4. Browser Support: smoothscroll focuses on providing a polyfill for older browsers, while vue-scrollto assumes modern browser capabilities.
  5. Use Case: Choose vue-scrollto for Vue.js projects with specific scrolling needs, and smoothscroll for broader compatibility or non-Vue projects.

Scrollytelling with IntersectionObserver.

Pros of Scrollama

  • Framework-agnostic, works with any JavaScript setup
  • Specialized for scroll-driven storytelling and data visualization
  • Offers step-based scrollytelling with progress tracking

Cons of Scrollama

  • More complex setup compared to Vue-scrollto
  • Focused on scrollytelling, less suitable for simple scroll-to-element functionality
  • Requires more custom JavaScript for basic scroll behaviors

Code Comparison

Vue-scrollto:

import VueScrollTo from 'vue-scrollto'
Vue.use(VueScrollTo)

// In component
this.$scrollTo('#element', 500, { easing: 'ease' })

Scrollama:

import scrollama from 'scrollama'

const scroller = scrollama()
scroller
  .setup({
    step: '.scroll-step'
  })
  .onStepEnter(response => {
    // Trigger animations or updates
  })

Vue-scrollto provides a simple API for scrolling to elements within Vue applications, while Scrollama offers more advanced scroll-driven interactions and storytelling capabilities. Vue-scrollto is easier to set up for basic scroll functionality, but Scrollama provides greater flexibility for complex scroll-based narratives and visualizations. The choice between them depends on the specific requirements of your project and the level of scroll interactivity needed.

The javascript library for magical scroll interactions.

Pros of ScrollMagic

  • More comprehensive animation control, allowing for complex scroll-based animations
  • Supports both vertical and horizontal scrolling scenarios
  • Compatible with multiple animation libraries (e.g., GSAP, Velocity.js)

Cons of ScrollMagic

  • Steeper learning curve due to its more complex API
  • Larger file size, which may impact page load times
  • Not specifically designed for Vue.js, requiring additional setup in Vue projects

Code Comparison

ScrollMagic:

var controller = new ScrollMagic.Controller();
var scene = new ScrollMagic.Scene({
    triggerElement: "#trigger",
    duration: 300
})
.setTween("#animate", {scale: 2.5})
.addTo(controller);

vue-scrollto:

import VueScrollTo from 'vue-scrollto'
Vue.use(VueScrollTo)

// In component
this.$scrollTo('#element', 500, {
    easing: 'ease-in',
    offset: -60
})

Key Differences

  • ScrollMagic is a more powerful and flexible solution for complex scroll-based animations, while vue-scrollto focuses on simple, Vue-specific scrolling functionality.
  • vue-scrollto is easier to integrate and use in Vue.js projects, with a simpler API and smaller footprint.
  • ScrollMagic offers more advanced features like pinning elements and parallax effects, which are not available in vue-scrollto.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

vue-scrollto

Vue 2.x Vue 3.x npm npm-downloads license

DEMO

Scrolling to elements was never this easy!

This is for vue 2.x and vue 3.x (since v2.19.0)

For vue 1.x use vue-scrollTo@1.0.1 (note the capital T) but keep in mind that the old version depends on jquery.

Under the hood

vue-scrollto uses window.requestAnimationFrame to perform the animations, thus giving the best performance.

Easing is done using bezier-easing - A well tested easing micro-library.

It even knows when the user interrupts, and doesn't force scrolling that would result in bad UX.

Installing

This package is available on npm.

If you used this package before, please ensure you are using the right one, since it has been renamed from `vue-scrollTo` to `vue-scrollto`

Using npm:

npm install --save vue-scrollto

Using yarn:

yarn add vue-scrollto

Directly include it in html:

<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-scrollto"></script>

When including it in html, it will automatically call `Vue.use` and also set a `VueScrollTo` variable that you can use!

Nuxt.js

Add vue-scrollto/nuxt to modules section of nuxt.config.js

{
    modules: [
        'vue-scrollto/nuxt',

        // Or if you have custom options...
        ['vue-scrollto/nuxt', { duration: 300 }],
    ]
}

Usage

vue-scrollto can be used either as a vue directive, or programatically from your javascript.

As a vue directive

var Vue = require('vue');
var VueScrollTo = require('vue-scrollto');

Vue.use(VueScrollTo)

// You can also pass in the default options
Vue.use(VueScrollTo, {
     container: "body",
     duration: 500,
     easing: "ease",
     offset: 0,
     force: true,
     cancelable: true,
     onStart: false,
     onDone: false,
     onCancel: false,
     x: false,
     y: true
 })

In case you are using the browser version (directly including the script on your page), you can set the defaults with

VueScrollTo.setDefaults({
    container: "body",
    duration: 500,
    lazy: false,
    easing: "ease",
    offset: 0,
    force: true,
    cancelable: true,
    onStart: false,
    onDone: false,
    onCancel: false,
    x: false,
    y: true
})
<a href="#" v-scroll-to="'#element'">Scroll to #element</a>

<div id="element">
    Hi. I'm #element.
</div>

If you need to customize the scrolling options, you can pass in an object literal to the directive:

<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>

Check out the Options section for more details about the available options.

Programmatically

var VueScrollTo = require('vue-scrollto');

var options = {
    container: '#container',
    easing: 'ease-in',
    lazy: false,
    offset: -60,
    force: true,
    cancelable: true,
    onStart: function(element) {
      // scrolling started
    },
    onDone: function(element) {
      // scrolling is done
    },
    onCancel: function() {
      // scrolling has been interrupted
    },
    x: false,
    y: true
}

var cancelScroll = VueScrollTo.scrollTo(element, duration, options)

// or alternatively inside your components you can use
cancelScroll = this.$scrollTo(element, duration, options)

// to cancel scrolling you can call the returned function
cancelScroll()

Options

el / element

The element you want to scroll to.

container

The container that has to be scrolled.

Default: body

duration

The duration (in milliseconds) of the scrolling animation.

Default: 500

easing

The easing to be used when animating. Read more in the Easing section.

Default: ease

lazy

By default targetX/targetY are calculated once at the start of a scroll, however if the target may shift around during the scroll - setting lazy to false will force recalculation of targetX/targetY at each scroll step.

Default: true

offset

The offset that should be applied when scrolling. This option accepts a callback function since v2.8.0.

Default: 0

force

Indicates if scrolling should be performed, even if the scroll target is already in view.

Default: true

cancelable

Indicates if user can cancel the scroll or not.

Default: true

onStart

A callback function that should be called when scrolling has started. Receives the target element as a parameter.

Default: noop

onDone

A callback function that should be called when scrolling has ended. Receives the target element as a parameter.

Default: noop

onCancel

A callback function that should be called when scrolling has been aborted by the user (user scrolled, clicked etc.). Receives the abort event and the target element as parameters.

Default: noop

x

Whether or not we want scrolling on the x axis

Default: false

y

Whether or not we want scrolling on the y axis

Default: true

Easing

Easing is calculated using bezier-easing so you can pass your own values into options.easing in the form of an array with 4 values, or you can use any of the default easings by referencing their names as strings (ease, linear, ease-in, ease-out, ease-in-out).

vue-scrollto uses the following values for the default easings:

let easings = {
    'ease': [0.25, 0.1, 0.25, 1.0],
    'linear': [0.00, 0.0, 1.00, 1.0],
    'ease-in': [0.42, 0.0, 1.00, 1.0],
    'ease-out': [0.00, 0.0, 0.58, 1.0],
    'ease-in-out': [0.42, 0.0, 0.58, 1.0]
}

Simultaneous Scrolling

If you need to scroll multiple containers simultaneously, you can import the scroller factory directly and create multiple instances. (Using the default scrollTo methods allows for only one scroll action at a time for performance reasons.)

import {scroller} from 'vue-scrollto/src/scrollTo'
const firstScrollTo = scroller()
const secondScrollTo = scroller()
firstScrollTo('#el1')
secondScrollTo('#el2')

License

MIT

NPM DownloadsLast 30 Days