cl-noticebar.uvue 5.4 KB

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