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"/>
相关推荐
一个处女座的程序猿O(∩_∩)O2 小时前
Uniapp 开发中遇到的坑与注意事项:全面指南
uni-app
Elena_Lucky_baby2 小时前
uniapp 网络请求封装(uni.request 与 uView-Plus)
uni-app
黑云压城After6 小时前
uniapp小程序自定义日历(签到、补签功能)
小程序·uni-app
烂蜻蜓6 小时前
Uniapp 设计思路全分享
前端·css·vue.js·uni-app·html
Elena_Lucky_baby6 小时前
uniapp封装请求
uni-app
尚学教辅学习资料7 小时前
基于SpringBoot+Vue+uniapp的高校招聘小程序+LW参考示例
spring boot·uni-app·招聘系统
岑梓铭9 小时前
uniapp邪门事件
uni-app
漫天绯羽10 小时前
uniapp 中使用天地图,安卓端、h5
uni-app
尚学教辅学习资料12 小时前
基于SpringBoot+vue+uniapp的智慧旅游小程序+LW示例参考
vue.js·spring boot·uni-app·旅游
烂蜻蜓16 小时前
前端已死?什么是前端
开发语言·前端·javascript·vue.js·uni-app