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