1、给滚动元素添加ref标记
<div ref="wrapper" }"><div>
2、监听滚动事件
mounted() {
this.$refs.wrapper.addEventListener('scroll', this.handelscroll)
},
3、vuex保存元素滚动的位置scrollTop
export default new Vuex.Store({
state: {
scrollTop: 0,
},
mutations: {
handleScroll(state, value) {
state.scrollTop = value
},
},
getters: {},
})
handelscroll() {
let scrollTop = this.$refs.wrapper.scrollTop
this.$store.commit('handleScroll', scrollTop)
},
4、返回时给滚动元素到指定位置
activated() {
this.$refs.wrapper.scrollTop = this.$store.state.scrollTop
},