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