JS实现
给滚动区域加个ref
<div ref="scrollListWrap">
...
</div>
初始化滚动条记录值
let scrollTop = 0;
生命周期函数
beforeDestroy() {
scrollTop = 0;
},
beforeRouteLeave(to, from, next) {
scrollTop = this.$refs.scrollListWrap.scrollTop;
next();
},
activated() {
this.$nextTick(() => {
this.$refs.scrollListWrap.scrollTop = scrollTop;
});
}
vue 指令实现
安装声明
1. 安装vue-keep-scroll-position:
npm i -S vue-keep-scroll-position
2. main.js中引入
import VueKeepScrollPosition from 'vue-keep-scroll-position'
Vue.use(VueKeepScrollPosition)
使用
<div v-keep-scroll-position>
...
</div>