vue scroller返回页面记住滚动位置的实例代码
程序员文章站
2024-01-24 08:33:40
1,设置keepalive:是否缓存
router:
{
path: '/actlist',
name: 'actlist',
component: ac...
1,设置keepalive:是否缓存
router: { path: '/actlist', name: 'actlist', component: actlist, meta: { keepalive: true } }
2,设置渲染页面
app.vue
<keep-alive> <router-view v-if="$route.meta.keepalive"></router-view> </keep-alive> <router-view v-if="!$route.meta.keepalive"></router-view>
3,记录滚动位置并赋值
beforerouteenter(to,from,next){ if(!sessionstorage.askpositon || from.path == '/'){//当前页面刷新不需要切换位置 sessionstorage.askpositon = ''; next(); }else{ next(vm => { if(vm && vm.$refs.my_scroller){//通过vm实例访问this settimeout(function () { vm.$refs.my_scroller.scrollto(0, sessionstorage.askpositon, true); },0)//同步转异步操作 } }) } }, beforerouteleave(to,from,next){//记录离开时的位置 sessionstorage.askpositon = this.$refs.my_scroller && this.$refs.my_scroller.getposition() && this.$refs.my_scroller.getposition().top; next() }
总结
以上所述是小编给大家介绍的vue scroller返回页面记住滚动位置的实例代码,希望对大家有所帮助