| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <template>
- <view class="cl-back-top-wrapper" :style="{ bottom }" @tap="toTop">
- <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 { getTabBarHeight, hasCustomTabBar } from "@/cool";
- import { computed, onMounted, ref, watch, type PropType } from "vue";
- import { usePage } from "../../hooks";
- defineOptions({
- name: "cl-back-top"
- });
- const props = defineProps({
- top: {
- type: Number as PropType<number | null>,
- default: null
- }
- });
- const { screenHeight } = uni.getWindowInfo();
- // cl-page 上下文
- const page = usePage();
- // 是否显示回到顶部按钮
- const visible = ref(false);
- // 底部距离
- const bottom = computed(() => {
- let h = 20;
- if (hasCustomTabBar()) {
- h += getTabBarHeight();
- }
- return h + "px";
- });
- // 控制是否显示
- function onVisible(top: number) {
- visible.value = top > screenHeight - 100;
- }
- // 回到顶部
- function toTop() {
- page.scrollToTop();
- }
- onMounted(() => {
- if (props.top != null) {
- // 监听参数变化
- watch(
- computed(() => props.top!),
- (top: number) => {
- onVisible(top);
- },
- {
- immediate: true
- }
- );
- } else {
- // 监听页面滚动
- page.onPageScroll((top) => {
- onVisible(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>
|