cl-page.uvue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view
  4. :style="{ flex: 1 }"
  5. :scroll-top="scrollViewTop"
  6. :scroll-with-animation="true"
  7. @scroll="onScroll"
  8. >
  9. <cl-back-top v-if="backTop" @tap="scrollToTop"></cl-back-top>
  10. <theme></theme>
  11. <ui></ui>
  12. <slot></slot>
  13. </scroll-view>
  14. <!-- #endif -->
  15. <!-- #ifndef APP -->
  16. <cl-back-top v-if="backTop" @tap="scrollToTop"></cl-back-top>
  17. <theme></theme>
  18. <ui></ui>
  19. <slot></slot>
  20. <!-- #endif -->
  21. </template>
  22. <script setup lang="ts">
  23. import { computed, onMounted, ref, watch } from "vue";
  24. import Theme from "./theme.uvue";
  25. import Ui from "./ui.uvue";
  26. import { locale, t } from "@/locale";
  27. import { config } from "@/config";
  28. import { router } from "@/cool";
  29. defineOptions({
  30. name: "cl-page"
  31. });
  32. defineProps({
  33. // 是否显示回到顶部按钮
  34. backTop: {
  35. type: Boolean,
  36. default: config.backTop
  37. }
  38. });
  39. // 滚动距离
  40. const scrollTop = ref(0);
  41. // scroll-view 滚动位置
  42. const scrollViewTop = ref(0);
  43. // view 滚动事件
  44. function onScroll(e: UniScrollEvent) {
  45. scrollTop.value = e.detail.scrollTop;
  46. }
  47. // 页面滚动事件
  48. onPageScroll((e) => {
  49. scrollTop.value = e.scrollTop;
  50. });
  51. // 滚动到指定位置
  52. function scrollTo(top: number) {
  53. // #ifdef H5
  54. window.scrollTo({ top, behavior: "smooth" });
  55. // #endif
  56. // #ifdef MP
  57. uni.pageScrollTo({
  58. scrollTop: top,
  59. duration: 300
  60. });
  61. // #endif
  62. // #ifdef APP
  63. scrollViewTop.value = top;
  64. // #endif
  65. }
  66. // 回到顶部
  67. function scrollToTop() {
  68. scrollTo(0 + Math.random() / 1000);
  69. }
  70. onMounted(() => {
  71. // 标题多语言
  72. // #ifdef H5 || APP
  73. watch(
  74. computed(() => locale.value),
  75. () => {
  76. const style = router.route()?.style;
  77. if (style != null) {
  78. if (style.navigationBarTitleText != null) {
  79. uni.setNavigationBarTitle({
  80. title: t((style.navigationBarTitleText as string).replaceAll("%", ""))
  81. });
  82. }
  83. }
  84. },
  85. {
  86. immediate: true
  87. }
  88. );
  89. // #endif
  90. });
  91. defineExpose({
  92. scrollTop,
  93. scrollTo,
  94. scrollToTop
  95. });
  96. </script>