cl-page.uvue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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"></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"></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, scroller } 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. // 触发滚动事件
  46. scroller.emit(e.detail.scrollTop);
  47. }
  48. // 页面滚动事件
  49. scroller.on((top) => {
  50. scrollTop.value = top;
  51. });
  52. // 滚动到指定位置
  53. function scrollTo(top: number) {
  54. // #ifdef H5
  55. window.scrollTo({ top, behavior: "smooth" });
  56. // #endif
  57. // #ifdef MP
  58. uni.pageScrollTo({
  59. scrollTop: top,
  60. duration: 300
  61. });
  62. // #endif
  63. // #ifdef APP
  64. scrollViewTop.value = top;
  65. // #endif
  66. }
  67. // 回到顶部
  68. function scrollToTop() {
  69. scrollTo(0 + Math.random() / 1000);
  70. }
  71. onMounted(() => {
  72. // 标题多语言
  73. // #ifdef H5 || APP
  74. watch(
  75. computed(() => locale.value),
  76. () => {
  77. const style = router.route()?.style;
  78. if (style != null) {
  79. if (style.navigationBarTitleText != null) {
  80. uni.setNavigationBarTitle({
  81. title: t((style.navigationBarTitleText as string).replaceAll("%", ""))
  82. });
  83. }
  84. }
  85. },
  86. {
  87. immediate: true
  88. }
  89. );
  90. // #endif
  91. });
  92. defineExpose({
  93. scrollTop,
  94. scrollTo,
  95. scrollToTop
  96. });
  97. </script>