vue-plugin-scroll-into-view
vue-plugin-scroll-into-view
vue-scroll-into-viewĀ is a simple Vue.js plugin that provides a convenient way to scroll an element into view with just a single line of code. Whether you have a long page with many sections, or you need to navigate to a specific part of the page, this plugin makes it easy to do so.
It's lighweight 0.3KB!
Features
Scrolls an element into the visible area of the browser window Supports scrolling to an element byĀ $refs
,Ā selector
,Ā VNode
, orĀ HTMLElement
Ā Allows for customization of scroll options, such as animation and alignment Easy to install and use in your Vue.js 2 or 3 project
Usage Examples
Inside your code you can do something like this:
Basic usage
`
Hello World
<table ref="table">
<!-- long content -->
</table>
<button @click="nextPage"> Next </button>
Installation
NPM
npm install vue-plugin-scroll-into-view
Yarn
yarn add --dev vue-plugin-scroll-into-view
Adding to your project
index.js
`import Vue from 'vue'; import VueScrollIntoView from 'vue-plugin-scroll-into-view';
Vue.use(VueScrollIntoView);`
Options
this.$scrollIntoView(ref, options);
Option | Type | Description | Default value |
---|---|---|---|
behavior | String | Defines the transition animation. One of "auto" or "smooth". | "auto" |
block | String | Defines vertical alignment. One of "start", "center", "end", or "nearest". | "start" |
inline | String | Defines horizontal alignment. One of "start", "center", "end", or "nearest". | "nearest" |
scrollMode | String | Defines the scrolling mode. One of "always", "if-needed", or "never". | "always" |
skipOverflowHiddenElements | Boolean | Whether to skip scrolling the ancestor elements with overflow: hidden. | false |
allowHorizontalScroll | Boolean | Whether to allow horizontal scrolling if the element is wider than the viewport. | false |
force | Boolean | Whether to always scroll the element, even if it's already in view. | false |
Options example
This will scroll the element with the ID my-section into view with default scroll options. You can also pass in custom scroll options if desired:
this.$scrollIntoView('#my-section', { behavior: 'smooth', block: 'center' });
License
The code is available under theĀ MITĀ license.
Contributing
We are open to contributions, seeĀ CONTRIBUTING.mdĀ for more info.
You need at least Node 18 to build the project