Commit f0c7a7aa authored by wanghuan's avatar wanghuan

Merge branch 'c_client_taro-pack' of...

Merge branch 'c_client_taro-pack' of http://gitlab2.dui88.com/qinhaitao/taobao-mini-template into c_client_taro-pack
parents 166ad3f2 3870608b
import React from 'react' import React, { useState, useEffect, useCallback, useRef } from 'react'
import { View, Text } from '@tarojs/components' import { View, Text } from '@tarojs/components'
import styles from './Barrage.module.less' import Taro from '@tarojs/taro'
class Barrage extends React.Component { import styles from './Barrage.module.less'
constructor(props) {
super(props)
// 页面数据存储
this.state = {
barrageWay: [],
barrageList: []
}
this.timer = null
}
componentDidMount() { const Barrage = props => {
this.createData() const {
const self = this dataList = [
setTimeout(() => { '1撒撒打算打算打算的',
self.addBarrageListObj() '2撒撒打算打算打算的',
}, 200) '3撒撒打算打算打算的',
} '4撒撒打算打算打算的',
'5撒撒打算打算打算的',
'3撒撒打算打算打算的',
'3撒撒打算打算打算的',
'3撒撒打算打算打算的',
'3撒撒打算打算打算的',
'3撒撒打算打算打算的',
'3撒撒打算打算打算的',
'3撒撒打算打算打算的'
],
interval = 5000,
width = 500,
trackCount = 1
} = props
componentWillUnmount() { const [ barrageList, setBarrageList ] = useState([])
clearInterval(this.timer) const [ animationData, setAnimationData ] = useState(null)
} const timer = useRef(null)
const nowIndex = useRef(0)
createData() { const createData = () => {
const { dataList, trackCount = 1 } = this.props const Tracks = Array.from({ length: trackCount }, (v, k) => (k + 1) * 40 - 40)
const Tracks = Array.from( // TODO 多轨道弹幕
{ length: trackCount }, setBarrageList(
(v, k) => (k + 1) * 40 - 40 dataList.map((item, index) => ({
)
this.setState({
barrageList: dataList.map((item, index) => ({
width: item.length * 17 + 5,
top: Tracks[index < Tracks.length ? index : index % trackCount], top: Tracks[index < Tracks.length ? index : index % trackCount],
time: 5,
context: item context: item
})) }))
}) )
} }
/* * const callback = useCallback(() => {
* 因为从后台获取到的是全部的数据,所以要把数据分开,让每条数据有先后之分 timer.current = setInterval(() => {
* 每隔一秒往barrageList 数组插入一条数据 setAnimationData(
* */ Taro.createAnimation()
addBarrageListObj() { .translateX(nowIndex.current === 0 ? 0 : (-width / 2) * nowIndex.current)
const self = this .step({
const { barrageList, barrageWay } = this.state duration: nowIndex.current === 0 ? 0 : 1000
let i = 0 })
this.timer = setInterval(function () { .export()
barrageWay.push(barrageList[i]) )
self.setState({ if (nowIndex.current < barrageList.length - 1) {
barrageWay: barrageWay nowIndex.current = nowIndex.current + 1
}) } else {
i++ nowIndex.current = 0
if (i === barrageList.length - 1) {
if (self.props.Infinite) {
i = 0
} else {
clearInterval(self.timer)
}
} }
}, 1500) }, interval + 1000)
} }, [ barrageList, width ])
useEffect(() => {
callback()
return () => clearInterval(timer.current)
}, [ callback ])
render() { useEffect(() => {
const { barrageWay } = this.state createData()
return ( return () => {}
<View className={styles['barrage-fly']}> }, [])
{barrageWay.map((item, i) => {
if (!item?.context) return null return (
<View id='barrage_wrap' style={`width:${width / 100}rem`} className={styles['barrage-wrap']}>
<View id='barrage_container' animation={animationData} className={styles['barrage-fly']}>
{barrageList.map((item, index) => {
return ( return (
<View <View id={`barrage-${index}`} key={index} style={`width:${width / 100}rem`} className={styles['barrage-textFly']}>
key={i}
className={styles['barrage-textFly']}
style={`animation: ${styles.first} ${item.time}s linear forwards; top: ${item.top}px`}
>
{/* 弹幕内容 */} {/* 弹幕内容 */}
<Text className={styles['barrage-text']}>{item.context}</Text> <Text className={styles['barrage-text']}>{item.context}</Text>
</View> </View>
) )
})} })}
</View> </View>
) </View>
} )
} }
export default Barrage export default Barrage
.barrage-wrap {
height: 64px;
border: 1px solid red;
overflow: hidden;
}
.barrage-fly { .barrage-fly {
position: relative; position: relative;
height: 100%; height: 100%;
width: 700px;
z-index: 3; z-index: 3;
display: flex;
}
.barrage-textFly { .barrage-textFly {
position: absolute; flex-shrink: 0;
height: 64px; height: 64px;
line-height: 64px; overflow: hidden;
color: #f9c797; line-height: 64px;
font-size: 32px; color: #f9c797;
padding-right: 30px; font-size: 32px;
} padding: 0 10px;
box-sizing: border-box;
text-align: center;
}
.barrage-text { .barrage-text {
white-space: nowrap; white-space: nowrap;
}
} }
@keyframes first { @keyframes first {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment