2019-12-11vue记录当前滚动列表位置,进入详情后再返回到列表当前位置

大家好,我本泽锅又来了,最近有个需求需要 一个界面,2个tab, 进入每个tab,点击进入详情,再返回到对应的tab,并且需要返回到浏览的位置。我是做的处理,先如图所示


image.png

1.首先处理两个tab的切换,点击进入详情,再返回到对应的tab,这个简单
在列表界面 借助 beforeRouteLeave 这个方法,记录路由离开的时候的tab

beforeRouteLeave(to, from, next) {
            console.log('beforeRouteLeave',to);
            setSession('allocationTab', this.selected);
             next();
        },

这里的setSession('allocationTab', this.selected); 是vuex 不懂的同学百度下
然后在界面 mounted() 方法里

mounted() {
            if(getSession('allocationTab')){
                    this.selected = getSession('allocationTab')
            }
            this.getData(1);
        },

这样就可以完美解决返回到对应的tab的问题
2.第二个就是解决记录滚动位置,再次返回时到之前浏览的位置
这里还是用到beforeRouteLeave这个方法

beforeRouteLeave (to, from, next) {
            if (this.status = 1){
                this.$refs.historyComp.recordCurrentTab();
            }else {
                this.$refs.workComp.recordCurrentTab();
            }
            setSession('allocationTab', this.selected);
            next();
        },

这里的 this.status = 1 记录的是当前切换的tab
this.$refs.historyComp.recordCurrentTab(); 是让列表的item记录当前滚动的位置
在item里的方法,首先要监听dom的滚动,然后在mountd方法里将每次的位置滑动动当前记录的位置

backScroll() {
                console.log('backScroll', getSession('StoreList_' + this.status))
                if (getSession('StoreList_' + this.status) && getSession('StoreList_' + this.status).pos > 0) {
                    setTimeout(() => {
                        document.getElementById('StoreList_' + this.status).scrollTop = getSession(
                            'StoreList_' + this.status).pos;
                        console.log('getElementById', getSession('StoreList_' + this.status).pos)
                    }, 500);
                }
            },
recordCurrentTab() {
                console.log('StoreList_' + this.status, document.getElementById('StoreList_' + this.status).scrollTop);
                setSession('StoreList_' + this.status, {
                    'pos': (document.getElementById('StoreList_' + this.status).scrollTop || 0)
                });
            },
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,761评论 0 17
  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,539评论 0 5
  • Documentation Supported OS & SDK Versions 支持的OS & SDK版本 S...
    c5550ea746f8阅读 4,483评论 0 2
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,174评论 1 32
  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 5,926评论 0 10