从详情页返回列表页记住滚动条位置

需求,从blog列表展示页进入单条blog详情页后返回不刷新,并且保留滚动条的位置。

1.返回不刷新使用keepAlive。(多级路由/菜单会导致keepAlive失效)

$route(to, from){
            if (to.meta.keepAlive){
                !this.include.includes(to.meta.name)&&this.include.push(to.meta.name);
            }
            if (from.meta.keepAlive && to.meta.deepth < from.meta.keepAlive){
                //清空from缓存,除了首页以外
                if (from.name === 'home'){
                    return ;
                }
                let index = this.include.indexOf(from.meta.name);
                index !== -1 && this.include.splice(index, 1);
            }
            if(to.meta.name&&to.meta.name.indexOf('handle')!=-1){//解决进入操作页返回不刷新问题
                this.include = []
            }
        }

2.在离开当前页面时获取当前滚动条高度。因为设置了keepAlive,所以可以在data中设置一个变量存储。

(ref加在产生滚动的那个容器上)

3.返回当前页面时在activated周期中设置保存的高度。

activated() {
        this.$refs.wrapper.scrollTop = this.scrollTop;
    },

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值