@@ -50,6 +50,7 @@ const InternalBarrage: ForwardRefRenderFunction<
50
50
const barrageContainer = useRef < HTMLDivElement > ( null )
51
51
const barrageCWidth = useRef ( 0 )
52
52
const timer = useRef ( 0 )
53
+ const domTimer = useRef ( 0 )
53
54
const index = useRef ( 0 )
54
55
const times = useRef < number [ ] > ( [ ] )
55
56
const historyIndex = useRef ( - 1 )
@@ -75,30 +76,35 @@ const InternalBarrage: ForwardRefRenderFunction<
75
76
return 0
76
77
}
77
78
79
+ const clearDomTimeout = ( ) => {
80
+ if ( domTimer . current ) {
81
+ clearTimeout ( domTimer . current )
82
+ domTimer . current = 0
83
+ }
84
+ }
85
+
78
86
useEffect ( ( ) => {
79
87
const init = async ( ) => {
80
88
if ( barrageBody . current ) {
81
89
barrageCWidth . current = await getNodeWidth ( barrageBody . current )
90
+ barrageBody . current ?. style . setProperty (
91
+ '--move-distance' ,
92
+ `-${ barrageCWidth . current } px`
93
+ )
94
+ index . current = 0
95
+ run ( )
82
96
}
83
97
}
84
-
85
- init ( )
86
-
87
- setTimeout ( ( ) => {
88
- barrageBody . current ?. style . setProperty (
89
- '--move-distance' ,
90
- `-${ barrageCWidth . current } px`
91
- )
92
- index . current = 0
93
- run ( )
98
+ domTimer . current = window . setTimeout ( ( ) => {
99
+ init ( )
94
100
} , 300 )
95
101
return ( ) => {
96
- clearInterval ( timer . current )
102
+ clearDomTimeout ( )
97
103
}
98
104
} , [ list ] )
99
105
100
106
const run = ( ) => {
101
- clearInterval ( timer . current )
107
+ clearTimeout ( timer . current )
102
108
let intervalCache = interval
103
109
const _index = ( loop ? index . current % list . length : index . current ) % rows
104
110
const result = times . current [ _index ] - rows * interval
@@ -110,7 +116,31 @@ const InternalBarrage: ForwardRefRenderFunction<
110
116
} , intervalCache )
111
117
}
112
118
113
- const play = async ( ) => {
119
+ const setStyle = async ( el : HTMLElement , currentIndex : number ) => {
120
+ try {
121
+ if ( el ) {
122
+ const refe = await getRectByTaro ( el )
123
+ const width = refe . width
124
+ const height = refe . height
125
+ el . classList . add ( 'move' )
126
+ const elScrollDuration = Math . round (
127
+ ( width / barrageCWidth . current ) * duration
128
+ )
129
+ times . current [ currentIndex ] = elScrollDuration
130
+ el . style . animationDuration = `${ duration + elScrollDuration } ms`
131
+ el . style . top = `${ currentIndex * ( height + gapY ) + 20 } px`
132
+ el . style . width = `${ width } px`
133
+ }
134
+ } catch ( error ) {
135
+ console . log ( '异常自动流转到下一个' , error )
136
+ ; ( barrageContainer . current as HTMLDivElement ) . removeChild ( el )
137
+ }
138
+ el . addEventListener ( 'animationend' , ( ) => {
139
+ ; ( barrageContainer . current as HTMLDivElement ) . removeChild ( el )
140
+ } )
141
+ }
142
+
143
+ const play = ( ) => {
114
144
if ( ! loop && index . current >= list . length ) {
115
145
return
116
146
}
@@ -130,26 +160,9 @@ const InternalBarrage: ForwardRefRenderFunction<
130
160
historyIndex . current = currentIndex
131
161
132
162
el . innerHTML = list [ _index ] as string
133
- el . classList . add ( 'barrage-item' )
134
163
; ( barrageContainer . current as HTMLDivElement ) . appendChild ( el )
135
-
136
- try {
137
- const width = await getNodeWidth ( el )
138
- const height = await getNodeWidth ( el , 'height' )
139
- el . classList . add ( 'move' )
140
- const elScrollDuration = ( width / barrageCWidth . current ) * duration
141
- times . current [ currentIndex ] = Math . ceil ( elScrollDuration )
142
- el . style . animationDuration = `${ ( duration + elScrollDuration ) . toFixed (
143
- 2
144
- ) } ms`
145
- el . style . top = `${ currentIndex * ( height + gapY ) + 20 } px`
146
- el . style . width = `${ width } px`
147
- } catch ( error ) {
148
- console . log ( '异常自动流转到下一个' )
149
- }
150
- el . addEventListener ( 'animationend' , ( ) => {
151
- ; ( barrageContainer . current as HTMLDivElement ) . removeChild ( el )
152
- } )
164
+ el . classList . add ( 'barrage-item' )
165
+ requestAnimationFrame ( ( ) => setStyle ( el , currentIndex ) )
153
166
index . current ++
154
167
run ( )
155
168
}
0 commit comments