使用 npm 或 yarn 安装:
npm install vue-scrollto
或
yarn add vue-scrollto
import Vue from 'vue';
import VueScrollTo from 'vue-scrollto';
Vue.use(VueScrollTo);
// 现在可以在任何组件中使用 v-scroll-to 指令
<template>
<div>
<button v-scroll-to="'#element'">Scroll to Element</button>
<div id="element" style="margin-top: 1000px;">Target Element</div>
</div>
</template>
<template>
<div>
<button v-scroll-to="{el: '#element', duration: 500, offset: -100}">Scroll to Element</button>
<div id="element" style="margin-top: 1000px;">Target Element</div>
</div>
</template>
this.$scrollTo('#element', 500, {
offset: -100,
easing: 'ease-in-out',
onDone: () => {
console.log('Scrolling done');
}
});
v-scroll-to 指令:用于在模板中添加滚动指令。this.$scrollTo 方法:以编程方式滚动到指定元素。el:目标元素的选择器或 DOM 元素。duration:滚动持续时间(毫秒)。offset:滚动偏移量(像素)。easing:滚动动画效果。onDone:滚动完成时的回调函数。onCancel:滚动取消时的回调函数。