添加回到顶部组件
This commit is contained in:
90
uni_modules/cool-ui/components/cl-back-top/cl-back-top.uvue
Normal file
90
uni_modules/cool-ui/components/cl-back-top/cl-back-top.uvue
Normal file
@@ -0,0 +1,90 @@
|
||||
<template>
|
||||
<view class="cl-back-top-wrapper" :style="{ bottom }">
|
||||
<view
|
||||
class="cl-back-top"
|
||||
:class="{
|
||||
'is-show': visible
|
||||
}"
|
||||
>
|
||||
<cl-icon name="skip-up-line" color="white" size="25px"></cl-icon>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref, watch, type PropType } from "vue";
|
||||
import { usePage } from "@/cool";
|
||||
|
||||
defineOptions({
|
||||
name: "cl-back-top"
|
||||
});
|
||||
|
||||
const props = defineProps({
|
||||
top: {
|
||||
type: Number as PropType<number | null>,
|
||||
default: null
|
||||
}
|
||||
});
|
||||
|
||||
const page = usePage();
|
||||
const { screenHeight } = uni.getWindowInfo();
|
||||
|
||||
// 是否显示回到顶部按钮
|
||||
const visible = ref(false);
|
||||
|
||||
// 底部距离
|
||||
const bottom = computed(() => {
|
||||
let h = 20;
|
||||
|
||||
if (page.hasCustomTabBar()) {
|
||||
h += page.getTabBarHeight();
|
||||
}
|
||||
|
||||
return h + "px";
|
||||
});
|
||||
|
||||
// 控制是否显示回到顶部按钮
|
||||
function update(top: number) {
|
||||
visible.value = top > screenHeight - 100;
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
if (props.top != null) {
|
||||
// 监听参数变化
|
||||
watch(
|
||||
computed(() => props.top!),
|
||||
(top: number) => {
|
||||
update(top);
|
||||
},
|
||||
{
|
||||
immediate: true
|
||||
}
|
||||
);
|
||||
} else {
|
||||
// 监听页面滚动
|
||||
page.onPageScroll((top) => {
|
||||
update(top);
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.cl-back-top {
|
||||
@apply flex flex-row items-center justify-center bg-primary-500 rounded-full;
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
transition-property: transform;
|
||||
transition-duration: 0.3s;
|
||||
transform: translateX(160rpx);
|
||||
|
||||
&.is-show {
|
||||
transform: translateX(-20px);
|
||||
}
|
||||
|
||||
&-wrapper {
|
||||
@apply fixed z-50 overflow-visible;
|
||||
right: 0;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user