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"/>
相关推荐
前端(从入门到入土)7 小时前
uniapp加载json动画
uni-app·json
peachSoda713 小时前
uniapp小程序生成海报/图片并保存分享
小程序·uni-app
奔跑吧邓邓子14 小时前
使用 Spring Boot 和 Uniapp 搭建 NFC 读取系统
spring boot·uni-app·nfc数据读取
sunly_21 小时前
uniapp:微信小程序,一键获取手机号
微信小程序·小程序·uni-app
Kx…………1 天前
Day2:前端项目uniapp壁纸实战
前端·学习·uni-app·实战·项目
高山流水&上善1 天前
uniapp地图导航及后台百度地图回显(v2/v3版本)
uni-app
Z编程1 天前
uniapp微信小程序引入vant组件库
微信小程序·小程序·uni-app
web_Hsir1 天前
vue + uniapp 实现仿百度地图/高德地图/美团/支付宝 滑动面板 纯css 实现
css·vue.js·uni-app
qq_316837752 天前
uniapp 打包 H5 向 打包的APP 使用 @dcloudio/uni-webview-js 传值
开发语言·javascript·uni-app
WeiAreYoung2 天前
uni-app ucharts自定义换行tooltips
uni-app