
Vue ScrollTo
A Vue.js directive that listens for click events and scrolls to elements
Scrolling to elements was never this easy! Scroll to any element on page, Scroll inside any container and Customize how scrolling behaves!
Installing
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!
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,
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,
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.
Programatically
var VueScrollTo = require('vue-scrollto');
var options = {
container: '#container',
easing: 'ease-in',
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()
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')




