问题:vue项目 滚动列表,当点击跳转到其他页面B,后再返回,需要滚动列表定位到之前的位置。
思路:记录当前滚动列表的位置,再还原
关键代码如下:
<div ref="container" class="container" @scroll="scrollHandler">
//... list
<div>.....
</div>
activated() {
this.$nextTick(() => {
// 设置滚动距离,实现返回定位
this.$refs.container.scrollTop = this.scrollTop;
});
},
// 滚动的监听回调函数
scrollHandler(event) {
this.scrollTop = event.target.scrollTop;
},