首页 > 其他 > 详细

vue吸顶组件

时间:2021-06-03 17:09:23      阅读:10      评论:0      收藏:0      [点我收藏+]
思路:
1、判断当前页面的滚动高度大于某个值时触发吸顶行为
2、样式 position: fixed; top: 0;width: 100%;,注意宽一定要设置100%,确保横向撑开
3、离开当前页面销毁destroyed 滚动事件
<template>
    <div class="nav-bar" :class="{ is_fixed: isFixed }">
        <div class="container">
            <div class="pro-title">
                小米8
            </div>
        </div>
    </div>
</template>


<script>
// @ is an alias to /src

export default {
    name: ‘nav-bar‘,
    data() {
        return {
            isFixed: false,
        };
    },
    components: {},
    mounted() {
        window.addEventListener(‘scroll‘, this.initHeight);
    },
    methods: {
        initHeight() {
            let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
            this.isFixed = scrollTop > 152 ? true : false;
        },
        // 当离开当前页面时会自动销毁this.initHeight,
        destroyed() {
            window.removeEventListener(‘scroll‘, this.initHeight, false);
        },
    },
};
</script>


<style lang="scss">
.nav-bar {
        height: 70px;
    line-height: 70px;
    border-top: 1px solid $colorH;
    background-color: #fff;
    &.is_fixed {
        position: fixed;
        top: 0;
        width: 100%;
        box-shadow: 0 5px 5px #ccc;
    }
}
</style>

vue吸顶组件

原文:https://www.cnblogs.com/kaicy/p/14845107.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!