Scrollspy, and animated scroll-to, for Vue2, extended vue2-scrollspy.
- Can specify scrolling containers, not just scroll window viewport
$ npm install @xlaoyu/vue-scrollspy --save
import Vue from 'vue';
import Scrollspy from '@xlaoyu/vue-scrollspy';
Vue.use(Scrollspy);
<ul v-scroll-spy-active v-scroll-spy-link>
<li>
<a>Menu 1</a>
</li>
<li>
<a>Menu 2</a>
</li>
</ul>
<div v-scroll-spy>
<div>
<h1>Header 1</h1>
<p>Content</p>
</div>
<div>
<h1>Header 2</h1>
<p>Content</p>
</div>
</div>
Declares container of sections for elements to scrollspy.
-
Use
v-scroll-spy="{data: 'section'}"
to add asection
data property in scope Vue instance that is binded to the section index. -
Use
v-scroll-spy="{allowNoActive: true}"
to allow no active sections when scroll position is outside of the scrollspy container. Default behavior is too keep active at least one section in any case. -
Use
v-scroll-spy="{offset: 50}"
to add an offset for scroll and active events. -
Use
v-scroll-spy="{time: 200, steps: 30}"
to set the animation options. -
$scrollTo(index: int)
is provided on scope Vue instance to invoke a scroll to the given section index. -
Use
v-scroll-spy="{ container: '.right' }"
specify scrolling containers, usedocument.querySelectorAll
to find containers.
Set the active
css class on element that match the index of current scrollspy.
- Use
v-scroll-spy-active="{selector: 'li.menu-item', class: 'custom-active'}"
to customize elements selection and class name to apply. By default, it will use direct children and applyactive
class.
Add click handler on children elements that will scroll to the related section.
- Use
v-scroll-spy-link="{selector: 'a.menu-link'}"
to customize elements selection. By default, it will usea
to select all links.
You should have the same number of children elements for v-scroll-spy
, v-scroll-spy-active
v-scroll-spy-link
for
directives to work properly.
If you need to share multiple scroll-spy in the same page, you can add data-scroll-spy-id=""
on each element where a
directive is declared.
If you want to set something else (not body) to scroll container, you could:
<div class="container" v-scroll-spy>
<div>
<h1>Header 1</h1>
<p>Content</p>
</div>
<div>
<h1>Header 2</h1>
<p>Content</p>
</div>
</div>
.container {
position: relative;
}
npm i && npm run watch
cd docs-src && npm i && npm run dev
MIT