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"/>
相关推荐
爱喝水的小周23 分钟前
《UniApp 页面配置文件pages.json》
前端·uni-app·json
iOS阿玮3 小时前
别问了,我自己的产品也卡审了44个小时!
uni-app·app·apple
2501_915918413 小时前
iOS描述文件功能解析
android·macos·ios·小程序·uni-app·cocoa·iphone
用户97141718142711 小时前
UniApp + Vue3 持久化登录(清除后台仍保持登陆状态)
uni-app
小白学鸿蒙12 小时前
新手记录使用uniapp-x开发鸿蒙应用
华为·uni-app·harmonyos
初遇你时动了情1 天前
uniapp/flutter中实现苹果IOS 26 毛玻璃效果、跟随滑动放大动画
flutter·ios·uni-app
gys98951 天前
uniapp使用sqlite模块
数据库·sqlite·uni-app
abigale031 天前
开发实战 - ego商城 -补充:使用uniapp扩展组件
uni-app·uni-ui
2501_916007471 天前
Fastlane 结合 开心上架(Appuploader)命令行实现跨平台上传发布 iOS App 的完整方案
android·ios·小程序·https·uni-app·iphone·webview
爱喝水的小周1 天前
《UniApp 页面导航跳转全解笔记》
前端·uni-app