1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
| <template>
| <view
| class="cl-noticebar"
| :class="[
| {
| 'is-scrollable': scrollable,
| },
| ]"
| :style="{
| color,
| backgroundColor,
| }"
| v-if="visible"
| >
| <cl-icon
| name="close-border"
| :size="34"
| :margin="[0, 10, 0, 0]"
| @tap="close"
| v-if="closeable"
| />
|
| <slot name="icon">
| <cl-icon :class-name="icon" :size="34" :margin="[0, 10, 0, 0]" v-if="icon" />
| </slot>
|
| <view class="cl-noticebar__box">
| <view
| class="cl-noticebar__scroller"
| :class="[`is-${direction}`]"
| :style="{
| height: parseRpx(height),
| top: scroll.top + 'px',
| left: scroll.left + 'px',
| transition,
| transform: `translateX(-${scroll.translateX}px)`,
| }"
| >
| <view class="cl-noticebar__item" v-for="(item, index) in list" :key="index">
| <text class="cl-noticebar__text">{{ item }}</text>
| </view>
| </view>
| </view>
| </view>
| </template>
|
| <script lang="ts">
| import {
| defineComponent,
| onMounted,
| onUnmounted,
| reactive,
| ref,
| computed,
| getCurrentInstance,
| type PropType,
| } from "vue";
| import { isArray } from "lodash-es";
| import { parseRpx } from "/@/cool/utils";
|
| export default defineComponent({
| name: "cl-noticebar",
|
| props: {
| // 文本内容
| text: {
| type: [String, Array],
| default: "",
| required: true,
| },
| // 方向
| direction: {
| type: String as PropType<"horizontal" | "vertical">,
| default: "horizontal",
| },
| // 字体颜色
| color: String,
| // 背景颜色
| backgroundColor: String,
| // 能否滚动
| scrollable: Boolean,
| // 滚动时长
| duration: {
| type: Number,
| default: 10,
| },
| // 能否关闭
| closeable: Boolean,
| // 左侧图标
| icon: String,
| // 高度
| height: {
| type: [String, Number],
| default: 40,
| },
| },
|
| emits: ["close"],
|
| setup(props, { emit }) {
| const { proxy }: any = getCurrentInstance();
|
| // 是否可见
| const visible = ref(true);
|
| // 滚动配置
| const scroll = reactive({
| left: 0,
| top: 0,
| translateX: 0,
| duration: 0,
| });
|
| // 计时器
| let timer: any = null;
|
| // 文案列表
| const list = computed(() => {
| return isArray(props.text) ? props.text : [props.text];
| });
|
| // 动画过度
| const transition = computed(() => {
| if (props.direction == "horizontal") {
| return `transform ${scroll.duration}s linear`;
| } else {
| return `top 0.3s`;
| }
| });
|
| // 刷新
| function refresh() {
| if (props.scrollable) {
| // 清除定时器
| clear();
|
| // 获取盒子大小
| uni.createSelectorQuery()
| .in(proxy)
| .select(`.cl-noticebar__box`)
| .boundingClientRect((box: any) => {
| // 获取文本大小
| uni.createSelectorQuery()
| .in(proxy)
| .select(`.cl-noticebar__text`)
| .boundingClientRect((text: any) => {
| const duration = props.duration * 1000;
|
| // 水平滑动
| if (props.direction == "horizontal") {
| const fn = () => {
| scroll.duration = props.duration;
| scroll.left = box.width;
| scroll.translateX = text.width + scroll.left;
|
| timer = setTimeout(() => {
| scroll.translateX = 0;
| scroll.duration = 0;
|
| setTimeout(fn, 500);
| }, duration);
| };
|
| fn();
| }
| // 垂直滑动
| else {
| timer = setInterval(() => {
| if (
| Math.abs(scroll.top) >=
| box.height * (list.value.length - 1)
| ) {
| scroll.top = 0;
| } else {
| scroll.top -= box.height;
| }
| }, duration);
| }
| })
| .exec();
| })
| .exec();
| }
| }
|
| // 关闭
| function close() {
| clear();
| visible.value = false;
| emit("close");
| }
|
| // 清空
| function clear() {
| clearInterval(timer);
| clearTimeout(timer);
| timer = null;
| }
|
| onMounted(() => {
| refresh();
| });
|
| onUnmounted(() => {
| clear();
| });
|
| return {
| visible,
| scroll,
| list,
| transition,
| refresh,
| close,
| clear,
| parseRpx,
| };
| },
| });
| </script>
|
|