uniapp uview 一键回到底部 组件

一、封装组件

javascript 复制代码
<template>
    <u-transition mode="fade" :show="show">
        <view class="back-bottom" @click="scrollToBottom">
            <u-icon name="arrow-downward" size="18" color="#909399"></u-icon>
            <text>底部</text>
        </view>
    </u-transition>
</template>

<script>
export default {
  name: "BackBottom",
  props: {
    bottom: {
        type: Number,
        default: 100
    }
  },
  data() {
    return {
        pageHeight: 0,
        scrollHeight: 0,
        diffHeight: this.bottom + 1, // 初始值大于bottom,防止首次渲染不显示
    };
  },
  computed: {
    show() {
        return this.diffHeight < this.bottom ? false : true;
    },
  },
  mounted() {
    this.pageHeight = document.documentElement.scrollHeight; // 当前页面高度
    window.addEventListener('scroll', this.updateScrollHeight);
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.updateScrollHeight);
  },
  methods: {
    updateScrollHeight() {
        let clientHeight = document.documentElement.clientHeight; // 当前屏幕高度
        this.scrollHeight = window.scrollY; // 页面滚动高度
        this.diffHeight = this.pageHeight - (this.scrollHeight + clientHeight);
    },
    scrollToBottom() {
        uni.pageScrollTo({
            scrollTop: this.pageHeight,
            duration: 100
        });
    }
  }
}
</script>

<style lang="scss" scoped>
.back-bottom{
    width: 40px;
    height: 40px;
    border-radius: 8rpx;
    background: #e1e1e1;
    position: fixed;
    bottom: 100rpx;
    right: 20px;
    box-shadow: 0px 0px 16px 1px rgba(119, 119, 119, 0.1);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 24rpx;
}        
</style>

二、使用组件

javascript 复制代码
<back-bottom />
<back-bottom bottom="100"/>
相关推荐
iOS阿玮3 小时前
分享一个4.3(a)瓜,一个操作毁了公司3个月的成果。
uni-app·app·apple
00后程序员张3 小时前
苹果软件混淆的工程逻辑,从符号空间到资源扰动的体系化实现
android·ios·小程序·https·uni-app·iphone·webview
街尾杂货店&9 小时前
word属性解释
uni-app
笨笨狗吞噬者19 小时前
【uniapp】小程序体积优化,JSON文件压缩
前端·微信小程序·uni-app
源码集结号20 小时前
一套智慧工地云平台源码,支持监管端、项目管理端,Java+Spring Cloud +UniApp +MySql技术开发
java·mysql·spring cloud·uni-app·源码·智慧工地·成品系统
木易 士心1 天前
Uni-App 实现多身份动态切换 TabBar 指南
uni-app
2501_915106322 天前
CDN 可以实现 HTTPS 吗?实战要点、部署模式与真机验证流程
网络协议·http·ios·小程序·https·uni-app·iphone
LoveEate3 天前
uniapp 运行/发版微信小程序
微信小程序·小程序·uni-app
fakaifa3 天前
【高级版】沃德政务招商系统源码+uniapp小程序
小程序·uni-app·源码下载·沃德政务招商系统·招商系统源码
weixin_446938873 天前
uniapp vue-i18n如何使用
前端·vue.js·uni-app