关于 element 的 backToTop

本文介绍如何使用Vue和Element UI实现一个带有返回顶部按钮的滚动监听效果。通过设置特定的滚动区域并利用Element UI的<el-backtop>组件,可以实现在页面滚动到一定位置时显示返回顶部按钮的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是一段完整的小demo。

效果图示例:
1195136-20190804150949952-208088124.jpg


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>test</title>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>

<style>
    * {
        box-sizing: border-box;
    }

    html,
    body {
        margin: 0;
        padding: 0;
    }

    .wraper {
        height: 100vh;
        border: 4px solid red;
        height: 500px;
        overflow: hidden;
        overflow-x: hidden;
        overflow-y: scroll;
    }

    .back-ball {
        background-color: tomato;
        color: #fff;
        border-radius: 22px;
        padding: 10px;
    }
</style>

<body>

    <div class="wraper">
        <template>
            Scroll down to see the bottom-right button.
            <el-backtop target=".wraper">
                <div class="back-ball">
                    回到顶部
                </div>
            </el-backtop>
        </template>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
        <h1>滚动内容部分</h1>
    </div>
</body>
<script>
    var vm = new Vue({
        el: '.wraper',
        data: {}
    })
</script>

</html>

** 注意: ** target挂载点需要是一个滚动区域(设置高度。overflow-y:scroll)

转载于:https://www.cnblogs.com/niluiquhz/p/11298281.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值