Skip to content

Commit c2e5e9b

Browse files
jasper-opsMoonofweisheng
authored andcommitted
feat: ✨ notice-bar增加多文本轮播功能
1 parent acf3f99 commit c2e5e9b

File tree

4 files changed

+89
-26
lines changed

4 files changed

+89
-26
lines changed

docs/component/notice-bar.md

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,45 @@
7979
/>
8080
```
8181

82+
## 多文本轮播
83+
将一个`string[]`传递给`text`属性,即可开启多文本轮播,并且会在展示下一条文本时触发`next`事件,该事件接收一个`number`参数,代表的是当前展示的文本数组索引
84+
85+
```html
86+
<wd-notice-bar :text="textArray" prefix="check-outline" @next="onNext" />
87+
```
88+
89+
```javascript
90+
import { ref } from 'vue'
91+
92+
const textArray = ref([
93+
'欢迎使用wot design uni',
94+
'该组件库基于uniapp ->Vue3, ts构建',
95+
'项目地址:https://github.com/Moonofweisheng/wot-design-uni',
96+
'我们的目标是打造最强uniapp组件库',
97+
'诚挚邀请大家共同建设'
98+
])
99+
100+
const onNext = (index: number) => {
101+
console.log('展示下一条,index: ', index)
102+
console.log('文本是:' + textArray.value[index])
103+
}
104+
```
105+
106+
82107
## Attributes
83108

84109
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
85110
| ---------------- | -------------------------------------- | ------- | ----------------------- | ------- | -------- |
86-
| text | 设置通知栏文案 | string | - | - | - |
87-
| type | 设置通知栏类型 | string | info / warning / danger | warning | - |
88-
| prefix | 设置左侧图标,使用 icon 章节中的图标名 | string | - | - | - |
89-
| scrollable | 是否可以滚动 | boolean | - | true | - |
90-
| delay | 滚动动画初始延时,单位 秒(s) | number | - | 1 | - |
91-
| speed | 滚动速度,单位 px/s | number | - | 50 | - |
92-
| closable | 是否可以关闭 | boolean | - | false | - |
93-
| wrapable | 是否换行展示 | boolean | - | false | - |
94-
| color | 文字、图标颜色 | string | - | - | - |
95-
| background-color | 背景颜色 | string | - | - | - |
111+
| text | 设置通知栏文案 | `string` `string[]` | - | - | - |
112+
| type | 设置通知栏类型 | `string` | info / warning / danger | warning | - |
113+
| prefix | 设置左侧图标,使用 icon 章节中的图标名 | `string` | - | - | - |
114+
| scrollable | 是否可以滚动 | `boolean` | - | true | - |
115+
| delay | 滚动动画初始延时,单位 秒(s) | `number` | - | 1 | - |
116+
| speed | 滚动速度,单位 px/s | `number` | - | 50 | - |
117+
| closable | 是否可以关闭 | `boolean` | - | false | - |
118+
| wrapable | 是否换行展示 | `boolean` | - | false | - |
119+
| color | 文字、图标颜色 | `string` | - | - | - |
120+
| background-color | 背景颜色 | `string` | - | - | - |
96121

97122
## Events
98123

src/pages/noticeBar/Index.vue

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @Date: 2023-06-13 11:47:12
44
* @LastEditTime: 2023-08-07 20:24:04
55
* @LastEditors: weisheng
6-
* @Description:
6+
* @Description:
77
* @FilePath: \wot-design-uni\src\pages\noticeBar\Index.vue
88
* 记得注释
99
-->
@@ -57,9 +57,28 @@
5757
background-color="#f0f9eb"
5858
></wd-notice-bar>
5959
</demo-block>
60+
61+
<demo-block title="多文本轮播">
62+
<wd-notice-bar :text="textArray" prefix="check-outline" @next="onNext" />
63+
</demo-block>
6064
</page-wraper>
6165
</template>
62-
<script lang="ts" setup></script>
66+
<script lang="ts" setup>
67+
import { ref } from 'vue'
68+
69+
const textArray = ref([
70+
'欢迎使用wot design uni',
71+
'该组件库基于uniapp ->Vue3, ts构建',
72+
'项目地址:https://github.com/Moonofweisheng/wot-design-uni',
73+
'我们的目标是打造最强uniapp组件库',
74+
'诚挚邀请大家共同建设'
75+
])
76+
77+
const onNext = (index: number) => {
78+
console.log('展示下一条,index: ', index)
79+
console.log('文本是:' + textArray.value[index])
80+
}
81+
</script>
6382
<style lang="scss" scoped>
6483
:deep(.prefix) {
6584
font-size: 18px;

src/uni_modules/wot-design-uni/components/wd-notice-bar/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { PropType } from 'vue'
12
import { baseProps, makeBooleanProp, makeNumberProp, makeStringProp } from '../common/props'
23

34
export type NoticeBarType = 'warning' | 'info' | 'danger' | ''
@@ -7,7 +8,10 @@ export const noticeBarProps = {
78
/**
89
* 设置通知栏文案
910
*/
10-
text: String,
11+
text: {
12+
type: [String, Array] as PropType<string | string[]>,
13+
default: ''
14+
},
1115
/**
1216
* 设置通知栏类型,可选值为:'warning' | 'info' | 'danger'
1317
*/

src/uni_modules/wot-design-uni/components/wd-notice-bar/wd-notice-bar.vue

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
<slot v-else name="prefix"></slot>
99
<view class="wd-notice-bar__wrap">
1010
<view class="wd-notice-bar__content" :animation="animation" @transitionend="animationEnd">
11-
{{ text }}
12-
<slot></slot>
11+
<slot>{{ currentText }}</slot>
1312
</view>
1413
</view>
1514
<wd-icon v-if="closable" custom-class="wd-notice-bar__suffix" size="18px" name="close-bold" @click="handleClose"></wd-icon>
@@ -33,22 +32,22 @@ import { getRect } from '../common/util'
3332
import { getCurrentInstance } from 'vue'
3433
import { nextTick } from 'vue'
3534
import { noticeBarProps } from './types'
35+
import { computed } from 'vue'
3636
3737
const $wrap = '.wd-notice-bar__wrap'
3838
const $content = '.wd-notice-bar__content'
3939
4040
const props = defineProps(noticeBarProps)
4141
42-
const firstPlay = ref<boolean>(true)
4342
const wrapWidth = ref<number>(0)
44-
const contentWidth = ref<number>(0)
4543
const show = ref<boolean>(true)
46-
const duration = ref<number>(0)
4744
const animation = ref<string>('')
4845
const noticeBarClass = ref<string>('')
46+
const currentIndex = ref(0)
47+
const textArray = computed(() => (Array.isArray(props.text) ? props.text : [props.text]))
48+
const currentText = computed(() => textArray.value[currentIndex.value])
4949
5050
const { proxy } = getCurrentInstance() as any
51-
5251
watch(
5352
[() => props.type, () => props.scrollable, () => props.wrapable],
5453
() => {
@@ -60,9 +59,7 @@ watch(
6059
watch(
6160
[() => props.text],
6261
() => {
63-
nextTick(() => {
64-
scroll()
65-
})
62+
nextTick(() => scroll())
6663
},
6764
{ deep: true, immediate: true }
6865
)
@@ -71,7 +68,7 @@ onBeforeMount(() => {
7168
computedClass()
7269
})
7370
74-
const emit = defineEmits(['close'])
71+
const emit = defineEmits(['close', 'next'])
7572
7673
function computedClass() {
7774
const { type, wrapable, scrollable } = props
@@ -104,18 +101,36 @@ function scroll() {
104101
105102
const wrapWidthTemp = wrapRect.width
106103
const contentWidthTemp = contentRect.width
107-
if (props.scrollable && contentWidthTemp > wrapWidthTemp) {
104+
if (props.scrollable) {
108105
animation.value = initAnimation((contentWidthTemp / props.speed) * 1000, props.delay * 1000, -contentWidthTemp)
109106
wrapWidth.value = wrapWidthTemp
110-
contentWidth.value = contentWidthTemp
111107
}
112108
})
113109
}
114110
111+
function next() {
112+
if (currentIndex.value >= textArray.value.length - 1) {
113+
currentIndex.value = 0
114+
} else {
115+
currentIndex.value++
116+
}
117+
emit('next', currentIndex.value)
118+
}
119+
115120
function animationEnd() {
116121
animation.value = initAnimation(0, 0, wrapWidth.value)
122+
117123
const timer = setTimeout(() => {
118-
animation.value = initAnimation(((wrapWidth.value + contentWidth.value) / props.speed) * 1000, 0, -contentWidth.value)
124+
next() // 更换下一条文本
125+
126+
nextTick(() => {
127+
// 因为文本会发生变化,所以contentWidth每一次都需要查询
128+
getRect($content, false, proxy).then((rect) => {
129+
if (!rect) return
130+
animation.value = initAnimation(((wrapWidth.value + rect.width!) / props.speed) * 1000, props.delay * 1000, -rect.width!)
131+
})
132+
})
133+
119134
clearTimeout(timer)
120135
}, 20)
121136
}

0 commit comments

Comments
 (0)