Skip to content

Commit 69d3c19

Browse files
authored
fix: 修改jd小程序高版本弹幕不滚动问题 (#2612)
* fix: 修改jd小程序高版本弹幕不滚动问题 * fix: 修改弹幕更新
1 parent ec2eec4 commit 69d3c19

File tree

2 files changed

+51
-33
lines changed

2 files changed

+51
-33
lines changed

src/packages/barrage/barrage.taro.tsx

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const InternalBarrage: ForwardRefRenderFunction<
5050
const barrageContainer = useRef<HTMLDivElement>(null)
5151
const barrageCWidth = useRef(0)
5252
const timer = useRef(0)
53+
const domTimer = useRef(0)
5354
const index = useRef(0)
5455
const times = useRef<number[]>([])
5556
const historyIndex = useRef(-1)
@@ -75,30 +76,35 @@ const InternalBarrage: ForwardRefRenderFunction<
7576
return 0
7677
}
7778

79+
const clearDomTimeout = () => {
80+
if (domTimer.current) {
81+
clearTimeout(domTimer.current)
82+
domTimer.current = 0
83+
}
84+
}
85+
7886
useEffect(() => {
7987
const init = async () => {
8088
if (barrageBody.current) {
8189
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()
8296
}
8397
}
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()
94100
}, 300)
95101
return () => {
96-
clearInterval(timer.current)
102+
clearDomTimeout()
97103
}
98104
}, [list])
99105

100106
const run = () => {
101-
clearInterval(timer.current)
107+
clearTimeout(timer.current)
102108
let intervalCache = interval
103109
const _index = (loop ? index.current % list.length : index.current) % rows
104110
const result = times.current[_index] - rows * interval
@@ -110,7 +116,31 @@ const InternalBarrage: ForwardRefRenderFunction<
110116
}, intervalCache)
111117
}
112118

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 = () => {
114144
if (!loop && index.current >= list.length) {
115145
return
116146
}
@@ -130,26 +160,9 @@ const InternalBarrage: ForwardRefRenderFunction<
130160
historyIndex.current = currentIndex
131161

132162
el.innerHTML = list[_index] as string
133-
el.classList.add('barrage-item')
134163
;(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))
153166
index.current++
154167
run()
155168
}

src/packages/barrage/demos/taro/demo1.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ const Demo1 = () => {
2020
return (
2121
<>
2222
<Cell className="barrage-demo-wrap">
23-
<Barrage className="barrage-demo" ref={barrageRef} list={barrageList} />
23+
<Barrage
24+
className="barrage-demo"
25+
interval={100}
26+
ref={barrageRef}
27+
list={barrageList}
28+
/>
2429
</Cell>
2530
<Button type="primary" onClick={addBarrage}>
2631
随机添加

0 commit comments

Comments
 (0)