美文网首页
vue场景——记录滚动高度

vue场景——记录滚动高度

作者: 张先觉 | 来源:发表于2020-07-13 15:26 被阅读0次

#思路:

路由跳转之前(或是Tab切换之前),记录一个高度值Value。之后,控制其滚动scrollTo(0, value),即可。

#知识点

  • keep-alive,router-view组件缓存,不重新加载。
  • activated钩子函数,当keep-alive缓存组件被切换,它的 activated 钩子函数将会被对应执行。
  • this.$nextTick(),等到DOM更新完成之后,然后,执行this.$nextTick,类似于Promise then()。此处,可以防止“Tab切换”时,无法滚动到底部的问题。

#代码

  • 场景1:路由切换时,记录滚动高度
<template>
    <div>
        <div class="about-content" ref="about"></div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            scrollHeight: 0,    // 记录滚动高度
        };
    },
    mounted() {
        this.aboutUs();
    },
    activated(){
        // 等到DOM更新完成之后,然后,执行this.$nextTick,类似于Promise then()
        this.$nextTick(() => {
            this.$refs.about.scrollTo(0, this.scrollHeight);
        })
    },
    beforeRouteLeave(to, from, next) {
        this.scrollHeight = this.$refs.about.scrollTop;
        next();
    },
};
</script>

<style scoped>
    .about-content{
        padding: 10px; background: #fff;
        overflow: auto;
        height: calc(100vh - 1.2rem); 
        -webkit-overflow-scrolling: touch;
    }

</style>
  • 场景2:tab切换时,记录滚动高度
<template>
    <div>
        <nav>
            <div :class="{'nav-item':true, 'active':isShow}" @click="navClickToggle(1)">盒子1</div>
            <div :class="{'nav-item':true, 'active':!isShow}" @click="navClickToggle(2)">盒子2</div>  
        </nav>

        <main class="main" ref="main">
            <!-- 盒子1 -->
            <section v-show="isShow"> </section>

            <!-- 盒子2 -->
            <section v-show="!isShow"></section>
        </main>
    </div>
</template>

<script>
export default {
    name: "Start",
    data() {
        return {
            isShow: true,
            mainScrollHeight:"",
            currentScrollHeight:0,
            willScrollHeight:0,
            
        }
    },
    activated: function () {
        // 记录滚动位置
        this.$refs.main.scrollTo(0,this.mainScrollHeight)
    },
    beforeRouteLeave(to, from, next) {
        this.mainScrollHeight = this.$refs.main.scrollTop;
        next();
    },
    methods: {
        /**
         *  导航栏点击切换,于此同时,记录滚动位置
         * @param flag 1子盒子   2子盒子
         */
        navClickToggle(flag) {
            this.isShow = !this.isShow;
            this.mainScrollHeight = this.$refs.main.scrollTop;
            if(flag === 1){
                this.$nextTick(function(){  
                    this.currentScrollHeight = this.mainScrollHeight;
                    this.$refs.main.scrollTo(0,this.willScrollHeight)
                })  
                
            }else if(flag === 2){
                this.$nextTick(function(){
                    this.willScrollHeight = this.mainScrollHeight;
                    this.$refs.main.scrollTo(0,this.currentScrollHeight);
                })
                
            }
        },
    },
}  
</script>

<style lang="scss" scoped>
.main {
    height: calc(100vh - 2.4rem);
    height: -moz-calc(100vh - 2.4rem);
    height: -webkit-calc(100vh - 2.4rem);
    overflow: auto;
    &::-webkit-scrollbar {
        width: 0;
        height: 0;
    }
}

</style>

相关文章

  • vue场景——记录滚动高度

    #思路: 路由跳转之前(或是Tab切换之前),记录一个高度值Value。之后,控制其滚动scrollTo(0, v...

  • 如何获取父级iframe的dom,并且设置高度

    场景:需要将vue嵌套在html页面的iframe中,根据vue页面的高度设置外层的iframe高度防止出现滚动条...

  • textarea有关

    vue自动适应高度,滚动 updated() { Autosize.update(this.$refs.tex...

  • 小程序滚动监听,并动画控制某元素的显示隐藏

    场景: 当页面滚动高度超过元素1高度时,元素2(动画的)显示或隐藏。在小程序中如何做到滚动监听并动态获取元素高度呢...

  • 2020-09-22

    VUE 1. vue 滚动行为用法,进入路由需要滚动到浏览器底部 头部等等 使用场景:使用前端路由,当切换到新路由...

  • 移动端禁止弹出层背景滚动

    场景:背景内容超出屏幕高度,需滚动展示;底部弹窗内容超出底部容器高度,需在容器内滚动展示;当底部弹窗打开,需阻止背...

  • pc端 jQuery实现 导航悬浮

    场景: 页面滚动距离超过banner的高度就header就悬浮 html javascript css 实现原理:...

  • vue 记录浏览位置

    vue中记录上浏览位置的方法场景描述: 在当前页面浏览时,点击进入其他页面,再返回时希望滚动到浏览时的位置 解决方...

  • 滑块and鼠标滚轮

    自定义滚动条的万能比例:**滚动条的高度 / 屏幕的高度 = 屏幕的高度 / 内容的高度 = 滚动条的移动距离 /...

  • JS 页面滚动,顶部显示加载进度

    实现思路:(将已经滚动的高度 / 可滚动的高度)* 100 效果: 案例:

网友评论

      本文标题:vue场景——记录滚动高度

      本文链接:https://www.haomeiwen.com/subject/pubacktx.html