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"/>
相关推荐
2501_916008891 天前
iOS 性能测试的深度实战方法 构建从底层指标到真实场景回放的多工具测试体系
android·ios·小程序·https·uni-app·iphone·webview
2501_915909062 天前
iOS APP 抓包全流程解析,HTTPS 调试、网络协议分析与多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
2501_915106322 天前
游戏上架 App Store 的技术流程解析 从构建到审核的全流程指南
游戏·macos·ios·小程序·uni-app·cocoa·iphone
行云流水6262 天前
uniapp pinia实现数据持久化插件
前端·javascript·uni-app
zhangyao9403302 天前
uniapp动态修改 顶部导航栏标题和右侧按钮权限显示隐藏
前端·javascript·uni-app
2501_916007472 天前
iOS 压力测试的工程化体系,构建高强度、多维度、跨工具协同的真实负载测试流程
android·ios·小程序·uni-app·cocoa·压力测试·iphone
2501_916008892 天前
API接口调试全攻略 Fiddler抓包工具、HTTPS配置与代理设置实战指南
前端·ios·小程序·https·fiddler·uni-app·webview
2501_915921432 天前
iOS 开发者工具推荐,构建从调试到性能优化的多维度生产力工具链(2025 深度工程向)
android·ios·性能优化·小程序·uni-app·iphone·webview
00后程序员张2 天前
全面解析网络抓包工具使用:Wireshark和TCPDUMP教程
网络·ios·小程序·uni-app·wireshark·iphone·tcpdump
游戏开发爱好者82 天前
Mac 抓包软件怎么选?从 HTTPS 调试、TCP 数据流分析到多工具协同的完整抓包方案
tcp/ip·macos·ios·小程序·https·uni-app·iphone