Vue3 Smooth Scroll
Lightweight Vue plugin for smooth-scrolling extended from vue2-smooth-scroll.
For simple use-cases, the native scroll-behavior
CSS property (working draft) may be enough.
Features
- Directive and programmatic API with global and local config overrides
- SSR
- Smooth, non-blocking animation using
requestAnimationFrame
(with fallback) - Y-axis or vertical scrolling
- Specific scroll containers
- 1.3kB gzipped, 2.9kB min
Installation
npm install --save vue3-smooth-scroll
import { createApp } from 'vue'
import VueSmoothScroll from 'vue3-smooth-scroll'
const app = createApp(...)
app.use(VueSmoothScroll)
Usage
Directive
<a href="#sec-3" v-smooth-scroll>Section 3</a>
<section id="sec-3"></section>
Programmatic
const myEl = this.$refs.myEl || this.$el || document.getElementById(...)
this.$smoothScroll({
scrollTo: myEl,
hash: '#sampleHash'
})
Direct in <script>
<body>
<script src="https://unpkg.com/vue@next"></script>
<script src="https://unpkg.com/vue3-smooth-scroll"></script>
<script>
const app = Vue.createApp(...)
app.use(VueSmoothScroll.default)
</script>
</body>
Custom options
Defaults
{
duration: 500,
offset: 0,
container: '',
updateHistory: true
easingFunction: null
}
Global
import { createApp } from 'vue'
import VueSmoothScroll from 'vue3-smooth-scroll'
const app = createApp(...)
app.use(VueSmoothScroll, {
duration: 400,
updateHistory: false,
})
Directive:
<div id="container">
<a href="#div-id" v-smooth-scroll="{ duration: 1000, offset: -50, container: '#container' }">Anchor</a>
<div id="div-id"></div>
</div>
Programmatic
this.$smoothScroll({
scrollTo: this.$refs.myEl,
duration: 1000,
offset: -50,
})
License
MIT