欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 高考 > uniapp uview 一键回到底部 组件

uniapp uview 一键回到底部 组件

2025/2/23 10:18:16 来源:https://blog.csdn.net/qq_40881695/article/details/143231982  浏览:    关键词:uniapp uview 一键回到底部 组件

一、封装组件

<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>

二、使用组件

<back-bottom />
<back-bottom bottom="100"/>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词