vue 一个页面切到另一个页面,再切回来的时候,滚动条的位置不变(只处理当前页面)

1、使用 beforeRouteLeave 来记录路由跳转之前滚动条的位置

beforeRouteLeave(to, from, next): 离开路由之前执行的函数,可用于页面的反向传值,页面跳转;
beforeRouteLeave(to,
from, next) { console.log('beforeRouteLeave.................') this.scroll = document.getElementsByClassName('el-main')[1].scrollTop //查找自己页面的滚动条 next() //一定不可漏写 },

2、在 activated 中将 beforeRouteLeave 中记录的位置赋值给引起滚动条滚动元素的 scrollTop

 

activated() {
  document.getElementsByClassName('el-main')[1].scrollTop = this.scroll
},

 

posted @ 2024-01-26 14:12  唏嘘-  阅读(147)  评论(0编辑  收藏  举报