uniapp 返回顶部

<template>
    <view>
        <view class="btn" @tap="toTop" :style="{'display':(flag===false? 'none':'block')}">
            <text class="cuIcon-top"></text>
        </view>
    </view>
</template>

<script>
    export default {
        name: "upTop",
        data() {
            return {
                flag: false,
            }
        },
        methods: {
            // 返回顶部
            toTop() {
                uni.pageScrollTo({
                    scrollTop: 0,
                    duration: 100,
                });
            },
            onPageScroll(e) { //根据距离顶部距离是否显示回到顶部按钮
                if (e.scrollTop > 10) { //当距离大于10时显示回到顶部按钮
                    this.flag = true
                } else {
                    this.flag = false
                }
            }
        }
    }
</script>

<style>
    .btn {
        position: fixed;
        z-index: 9999;
        right: 16px;
        bottom: 100px;
        background-color: #007AFF;
        padding: 5px;
        display: none;
        border-radius: 4px;

    }

    .btn .cuIcon-top {
        font-size: 30px;
        color: #FFFFFF;
    }
</style>

 

posted @ 2020-07-05 15:46  奔跑的前端猿  阅读(7806)  评论(1编辑  收藏  举报