仿bilibili回到顶部效果

废话不多说直接看效果

1.首先是监听鼠标滚动到页面顶部的距离

在合适的距离让返回顶部的按钮显示出来

    mounted() {
        window.addEventListener("scroll", this.handleScroll);
    },
    methods: {
        handleScroll() {
            //滚动条滚动时,距离顶部的距离
            this.scrollTop =
                window.pageYOffset ||
                document.documentElement.scrollTop ||
                document.body.scrollTop;
            // 判断是否下拉了所需长(高)度,若满足,则返回顶部图标出现
            if (this.scrollTop > document.body.clientHeight - 50) {
                this.isActive = true;
            } else {
                this.isActive = false;
            }
        },
    }
 2.显示出来后,添加点击事件,完成回到顶部操作
<script>
    methods: {
        //返回顶部
        goTop() {
            let top = document.documentElement.scrollTop || document.body.scrollTop;
            // 实现滚动效果
            const timeTop = setInterval(() => {
                document.body.scrollTop =
                    document.documentElement.scrollTop =
                    top -=
                    50;
                if (top <= 0) {
                    clearInterval(timeTop);
                }
            }, 10);
        },
    }

</style>

完整代码如下: 

<template>
    <div id="app">
        <h1>回到顶部组件</h1>
        <div class="scrollBox" title="返回顶部" @click="goTop()" v-show="isActive">
            <button class="primary-btn">
                <div class="div_icon"></div>
                <span class="primary-btn-text">顶部</span>
            </button>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            isActive: false,
        }
    },
    mounted() {
        window.addEventListener("scroll", this.handleScroll);
    },
    methods: {
        //返回顶部
        goTop() {
            let top = document.documentElement.scrollTop || document.body.scrollTop;
            // 实现滚动效果
            const timeTop = setInterval(() => {
                document.body.scrollTop =
                    document.documentElement.scrollTop =
                    top -=
                    50;
                if (top <= 0) {
                    clearInterval(timeTop);
                }
            }, 10);
        },
        handleScroll() {
            //滚动条滚动时,距离顶部的距离
            this.scrollTop =
                window.pageYOffset ||
                document.documentElement.scrollTop ||
                document.body.scrollTop;
            // 判断是否下拉了所需长(高)度,若满足,则返回顶部图标出现
            if (this.scrollTop > document.body.clientHeight - 50) {
                this.isActive = true;
            } else {
                this.isActive = false;
            }
        },
    }
}
</script>
<style lang="less" scoped>
#app {
    background: pink;
    width: 100%;
    height: 10000px;
}

.scrollBox {
    width: 40px;
    position: fixed;
    left: 50%;
    bottom: 60px;
    z-index: 100;
    margin-left: 530px;
    width: 35px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    text-align: center;
    border-radius: 8px;

    .primary-btn {
        width: 35px;
        padding: 3px 0 6px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border-radius: 8px;
        font-size: 12px;
        transform-origin: center;
        transition: 0.2s;
        border: 1px solid #e3e5e7;
        background: transparent;
        cursor: pointer;

        .div_icon {
            width: 0;
            height: 0;
            line-height: 0;
            font-size: 0;
            border: 5px solid transparent;
            border-bottom-color: #000;
        }

        .primary-btn-text {
            margin-top: 5px;
            text-align: center;
            font-size: 12px;
            line-height: 14px;
        }

        &:hover {
            background: #e3e5e7;
        }
    }
}
</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值