cl-noticebar.uvue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <template>
  2. <view
  3. class="cl-noticebar"
  4. :class="[pt.className]"
  5. :style="{
  6. height: parseRpx(height!)
  7. }"
  8. >
  9. <view class="cl-noticebar__scroller" :class="[`is-${direction}`]" :style="scrollerStyle">
  10. <view
  11. v-for="(item, index) in list"
  12. :key="index"
  13. class="cl-noticebar__item"
  14. :style="{
  15. height: parseRpx(height!)
  16. }"
  17. >
  18. <slot name="text" :item="item">
  19. <text
  20. class="cl-noticebar__text dark:!text-white"
  21. :class="[pt.text?.className]"
  22. >{{ item }}</text
  23. >
  24. </slot>
  25. </view>
  26. </view>
  27. </view>
  28. </template>
  29. <script setup lang="ts">
  30. import {
  31. onMounted,
  32. onUnmounted,
  33. reactive,
  34. computed,
  35. getCurrentInstance,
  36. type PropType,
  37. watch
  38. } from "vue";
  39. import { parseRpx, parsePt } from "@/cool";
  40. import type { PassThroughProps } from "../../types";
  41. defineOptions({
  42. name: "cl-noticebar"
  43. });
  44. defineSlots<{
  45. text(props: { item: string }): any;
  46. }>();
  47. const props = defineProps({
  48. // 样式穿透对象,允许外部自定义样式
  49. pt: {
  50. type: Object,
  51. default: () => ({})
  52. },
  53. // 公告文本内容,支持字符串或字符串数组
  54. text: {
  55. type: [String, Array] as PropType<string | string[]>,
  56. default: ""
  57. },
  58. // 滚动方向,支持 horizontal(水平)或 vertical(垂直)
  59. direction: {
  60. type: String as PropType<"horizontal" | "vertical">,
  61. default: "horizontal"
  62. },
  63. // 垂直滚动时的切换间隔,单位:毫秒
  64. duration: {
  65. type: Number,
  66. default: 3000
  67. },
  68. // 水平滚动时的速度,单位:px/s
  69. speed: {
  70. type: Number,
  71. default: 100
  72. },
  73. // 公告栏高度,支持字符串或数字
  74. height: {
  75. type: [String, Number] as PropType<string | number>,
  76. default: 40
  77. }
  78. });
  79. // 事件定义,当前仅支持 close 事件
  80. const emit = defineEmits(["close"]);
  81. // 获取当前组件实例,用于后续 DOM 查询
  82. const { proxy } = getCurrentInstance()!;
  83. // 获取设备屏幕信息
  84. const { windowWidth } = uni.getWindowInfo();
  85. // 样式透传类型定义
  86. type PassThrough = {
  87. className?: string;
  88. text?: PassThroughProps;
  89. };
  90. // 计算样式透传对象,便于样式自定义
  91. const pt = computed(() => parsePt<PassThrough>(props.pt));
  92. // 滚动相关状态,包含偏移量、动画时长等
  93. type Scroll = {
  94. left: number;
  95. top: number;
  96. translateX: number;
  97. duration: number;
  98. };
  99. const scroll = reactive<Scroll>({
  100. left: windowWidth,
  101. top: 0,
  102. translateX: 0,
  103. duration: 0
  104. });
  105. // 定时器句柄,用于控制滚动动画
  106. let timer: number = 0;
  107. // 公告文本列表,统一为数组格式,便于遍历
  108. const list = computed<string[]>(() => {
  109. return Array.isArray(props.text) ? (props.text as string[]) : [props.text as string];
  110. });
  111. // 滚动容器样式,动态计算滚动动画相关样式
  112. const scrollerStyle = computed(() => {
  113. const style = {};
  114. if (props.direction == "horizontal") {
  115. style["left"] = `${scroll.left}px`;
  116. style["transform"] = `translateX(-${scroll.translateX}px)`;
  117. style["transition-duration"] = `${scroll.duration}ms`;
  118. } else {
  119. style["transform"] = `translateY(${scroll.top}px)`;
  120. }
  121. return style;
  122. });
  123. // 清除定时器,防止内存泄漏或重复动画
  124. function clear(): void {
  125. if (timer != 0) {
  126. clearInterval(timer);
  127. clearTimeout(timer);
  128. timer = 0;
  129. }
  130. }
  131. // 刷新滚动状态
  132. function refresh() {
  133. // 先清除已有定时器,避免重复动画
  134. clear();
  135. // 查询公告栏容器尺寸
  136. uni.createSelectorQuery()
  137. .in(proxy)
  138. .select(".cl-noticebar")
  139. .boundingClientRect((box) => {
  140. // 获取容器高度和宽度
  141. const boxHeight = (box as NodeInfo).height ?? 0;
  142. const boxWidth = (box as NodeInfo).width ?? 0;
  143. // 查询文本节点尺寸
  144. uni.createSelectorQuery()
  145. .in(proxy)
  146. .select(".cl-noticebar__text")
  147. .boundingClientRect((text) => {
  148. // 水平滚动逻辑
  149. if (props.direction == "horizontal") {
  150. // 获取文本宽度
  151. const textWidth = (text as NodeInfo).width ?? 0;
  152. // 启动水平滚动动画
  153. function next() {
  154. // 计算滚动距离和动画时长
  155. scroll.translateX = textWidth + boxWidth;
  156. scroll.duration = Math.ceil((scroll.translateX / props.speed) * 1000);
  157. scroll.left = boxWidth;
  158. // 动画结束后重置,形成循环滚动
  159. // @ts-ignore
  160. timer = setTimeout(() => {
  161. scroll.translateX = 0;
  162. scroll.duration = 0;
  163. setTimeout(() => {
  164. next();
  165. }, 100);
  166. }, scroll.duration);
  167. }
  168. next();
  169. }
  170. // 垂直滚动逻辑
  171. else {
  172. // 定时切换文本,循环滚动
  173. // @ts-ignore
  174. timer = setInterval(() => {
  175. if (Math.abs(scroll.top) >= boxHeight * (list.value.length - 1)) {
  176. scroll.top = 0;
  177. } else {
  178. scroll.top -= boxHeight;
  179. }
  180. }, props.duration);
  181. }
  182. })
  183. .exec();
  184. })
  185. .exec();
  186. }
  187. onMounted(() => {
  188. watch(
  189. computed(() => props.text!),
  190. () => {
  191. refresh();
  192. },
  193. {
  194. immediate: true
  195. }
  196. );
  197. });
  198. onUnmounted(() => {
  199. clear();
  200. });
  201. </script>
  202. <style lang="scss" scoped>
  203. .cl-noticebar {
  204. &__scroller {
  205. @apply flex;
  206. transition-property: transform;
  207. transition-timing-function: linear;
  208. &.is-horizontal {
  209. @apply flex-row;
  210. overflow: visible;
  211. }
  212. &.is-vertical {
  213. @apply flex-col;
  214. transition-duration: 0.5s;
  215. }
  216. }
  217. &__item {
  218. @apply flex flex-row items-center;
  219. }
  220. &__text {
  221. @apply text-md text-surface-700;
  222. white-space: nowrap;
  223. }
  224. }
  225. </style>