11<template >
2- <view
3- v-if =" show"
4- :class =" `wd-notice-bar ${customClass} ${noticeBarClass}`"
5- :style =" `color: ${color}; background: ${backgroundColor};${customStyle}`"
6- >
2+ <view v-if =" show" :class =" `wd-notice-bar ${customClass} ${noticeBarClass}`" :style =" rootStyle" >
73 <wd-icon v-if =" prefix" custom-class =" wd-notice-bar__prefix" size =" 18px" :name =" prefix" ></wd-icon >
84 <slot v-else name =" prefix" ></slot >
95 <view class =" wd-notice-bar__wrap" >
10- <view class =" wd-notice-bar__content" :animation =" animation" @transitionend =" animationEnd" >
6+ <view class =" wd-notice-bar__content" :style =" animation" @transitionend =" animationEnd" >
117 <template v-if =" isVertical " >
12- <slot v-for =" (item, i) in textArray" :key =" item" name =" vertical" :item =" item" :index =" i" >
13- <view >{{ item }}</view >
14- </slot >
15- <slot v-if =" textArray.length > 1" name =" vertical" :item =" textArray[0]" :index =" 0" >
16- <view >{{ textArray[0] }}</view >
17- </slot >
8+ <view v-for =" item in textArray" :key =" item" >{{ item }}</view >
9+ <view v-if =" textArray.length > 1" >{{ textArray[0] }}</view >
1810 </template >
1911 <slot v-else >{{ currentText }}</slot >
2012 </view >
@@ -35,12 +27,9 @@ export default {
3527 </script >
3628
3729<script lang="ts" setup>
38- import { onBeforeMount , ref , watch } from ' vue'
39- import { getRect } from ' ../common/util'
40- import { getCurrentInstance } from ' vue'
41- import { nextTick } from ' vue'
30+ import { ref , watch , nextTick , computed , getCurrentInstance , type CSSProperties } from ' vue'
31+ import { getRect , isDef , objToStyle } from ' ../common/util'
4232import { noticeBarProps } from ' ./types'
43- import { computed } from ' vue'
4433
4534const $wrap = ' .wd-notice-bar__wrap'
4635const $content = ' .wd-notice-bar__content'
@@ -51,37 +40,27 @@ const emit = defineEmits(['close', 'next'])
5140const wrapWidth = ref <number >(0 )
5241const show = ref <boolean >(true )
5342const animation = ref <string >(' ' )
54- const noticeBarClass = ref <string >(' ' )
5543const currentIndex = ref (0 )
5644const textArray = computed (() => (Array .isArray (props .text ) ? props .text : [props .text ]))
5745const currentText = computed (() => textArray .value [currentIndex .value ])
5846const verticalIndex = ref (0 )
5947const isHorizontal = computed (() => props .direction === ' horizontal' )
6048const isVertical = computed (() => props .direction === ' vertical' )
6149
62- const { proxy } = getCurrentInstance () as any
63- watch (
64- [() => props .type , () => props .scrollable , () => props .wrapable ],
65- () => {
66- computedClass ()
67- },
68- { deep: true , immediate: true }
69- )
50+ const rootStyle = computed (() => {
51+ const style: CSSProperties = {}
52+ if (isDef (props .color )) {
53+ style .color = props .color
54+ }
7055
71- watch (
72- [() => props .text ],
73- () => {
74- nextTick (() => scroll ())
75- },
76- { deep: true , immediate: true }
77- )
56+ if (isDef (props .backgroundColor )) {
57+ style .background = props .backgroundColor
58+ }
7859
79- onBeforeMount (() => {
80- computedClass ()
60+ return ` ${objToStyle (style )};${props .customStyle } `
8161})
82-
83- function computedClass() {
84- const { type, wrapable, scrollable, direction } = props
62+ const noticeBarClass = computed (() => {
63+ const { type, wrapable, scrollable } = props
8564
8665 let noticeBarClasses: string [] = []
8766 type && noticeBarClasses .push (` is-${type } ` )
@@ -93,22 +72,34 @@ function computedClass() {
9372 }
9473
9574 wrapable && ! scrollable && noticeBarClasses .push (' wd-notice-bar--wrap' )
96- noticeBarClass .value = noticeBarClasses .join (' ' )
97- }
75+ return noticeBarClasses .join (' ' )
76+ })
77+
78+ const { proxy } = getCurrentInstance () as any
79+
80+ watch (
81+ [() => props .text ],
82+ () => {
83+ nextTick (() => scroll ())
84+ },
85+ { deep: true , immediate: true }
86+ )
87+
9888function handleClose() {
9989 show .value = false
10090 emit (' close' )
10191}
10292
10393function initAnimation({ duration , delay , translate }: { duration: number ; delay: number ; translate: number }) {
104- const ani = uni
105- .createAnimation ({
106- duration ,
107- delay
108- })
109- [isHorizontal .value ? ' translateX' : ' translateY' ](translate )
110- ani .step ()
111- return ani .export ()
94+ const style: CSSProperties = {
95+ transitionProperty: ' all' ,
96+ transitionDelay: ` ${delay }s ` ,
97+ transitionDuration: ` ${duration }s ` ,
98+ transform: ` ${props .direction === ' vertical' ? ' translateY' : ' translateX' }(${translate }px) ` ,
99+ transitionTimingFunction: ' linear'
100+ }
101+
102+ return objToStyle (style )
112103}
113104
114105function queryRect() {
@@ -117,10 +108,9 @@ function queryRect() {
117108
118109async function verticalAnimate(height : number ) {
119110 const translate = - (height / (textArray .value .length + 1 )) * (currentIndex .value + 1 )
120-
121111 animation .value = initAnimation ({
122- duration: props . speed * 1000 ,
123- delay: props .delay * 1000 ,
112+ duration: height / ( textArray . value . length + 1 ) / props . speed ,
113+ delay: props .delay ,
124114 translate
125115 })
126116}
@@ -134,8 +124,8 @@ async function scroll() {
134124 if (isHorizontal .value ) {
135125 if (props .scrollable ) {
136126 animation .value = initAnimation ({
137- duration: ( contentRect .width / props .speed ) * 1000 ,
138- delay: props .delay * 1000 ,
127+ duration: contentRect .width / props .speed ,
128+ delay: props .delay ,
139129 translate: - contentRect .width
140130 })
141131 }
@@ -183,8 +173,8 @@ function animationEnd() {
183173
184174 if (isHorizontal .value ) {
185175 animation .value = initAnimation ({
186- duration: (( wrapWidth .value + contentRect .width ) / props .speed ) * 1000 ,
187- delay: props .delay * 1000 ,
176+ duration: (wrapWidth .value + contentRect .width ) / props .speed ,
177+ delay: props .delay ,
188178 translate: - contentRect .width
189179 })
190180 } else {
0 commit comments