back-top.uvue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <template>
  2. <view class="back-top-wrapper" :style="{ bottom }">
  3. <view
  4. class="back-top"
  5. :class="{
  6. 'is-show': visible
  7. }"
  8. >
  9. <cl-icon name="skip-up-line" color="white" size="25px"></cl-icon>
  10. </view>
  11. </view>
  12. </template>
  13. <script setup lang="ts">
  14. import { computed, ref } from "vue";
  15. import { usePage } from "@/cool";
  16. defineOptions({
  17. name: "cl-page-back-top"
  18. });
  19. const page = usePage();
  20. const { screenHeight } = uni.getWindowInfo();
  21. // 是否显示回到顶部按钮
  22. const visible = ref(false);
  23. // 底部距离
  24. const bottom = computed(() => {
  25. let h = 20;
  26. if (page.hasCustomTabBar()) {
  27. h += page.getTabBarHeight();
  28. }
  29. return h + "px";
  30. });
  31. // 监听页面滚动
  32. page.onPageScroll((top) => {
  33. // 控制是否显示回到顶部按钮
  34. visible.value = top > screenHeight - 100;
  35. });
  36. </script>
  37. <style lang="scss" scoped>
  38. .back-top {
  39. @apply flex flex-row items-center justify-center bg-primary-500 rounded-full;
  40. width: 40px;
  41. height: 40px;
  42. transition-property: transform;
  43. transition-duration: 0.3s;
  44. transform: translateX(160rpx);
  45. &.is-show {
  46. transform: translateX(-20px);
  47. }
  48. &-wrapper {
  49. @apply fixed z-50 overflow-visible;
  50. right: 0;
  51. }
  52. }
  53. </style>