提交 10106795 编写于 作者: L liuyijun

fix: review修复barrage组件

上级 378732e1
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
overflow: hidden; overflow: hidden;
box-sizing: border-box; box-sizing: border-box;
background-color: #f7f8fa; background-color: #f7f8fa;
.dmitem { .barrage-item {
width: 100px; width: 100px;
// max-width: 150px; // max-width: 150px;
display: block; display: block;
......
...@@ -6,7 +6,7 @@ import './barrage.scss' ...@@ -6,7 +6,7 @@ import './barrage.scss'
export interface BarrageProps { export interface BarrageProps {
className: string className: string
style: React.CSSProperties style: React.CSSProperties
danmu: Array<string> barrageList: Array<string>
frequency: number frequency: number
loop: boolean loop: boolean
speeds: number speeds: number
...@@ -14,7 +14,7 @@ export interface BarrageProps { ...@@ -14,7 +14,7 @@ export interface BarrageProps {
top: number top: number
} }
const defaultProps = { const defaultProps = {
danmu: [''], barrageList: [],
frequency: 500, frequency: 500,
loop: true, loop: true,
speeds: 2000, speeds: 2000,
...@@ -22,13 +22,13 @@ const defaultProps = { ...@@ -22,13 +22,13 @@ const defaultProps = {
top: 10, top: 10,
} }
const InternalBarrage: ForwardRefRenderFunction<unknown, Partial<BarrageProps>> = (props, ref) => { const InternalBarrage: ForwardRefRenderFunction<unknown, Partial<BarrageProps>> = (props, ref) => {
const { className, frequency, loop, danmu, speeds, rows, top, ...restProps } = { const { className, frequency, loop, barrageList, speeds, rows, top, ...restProps } = {
...defaultProps, ...defaultProps,
...props, ...props,
} }
const dmBody = useRef<HTMLDivElement>(null) const barrageBody = useRef<HTMLDivElement>(null)
const dmContainer = useRef<HTMLDivElement>(null) const barrageContainer = useRef<HTMLDivElement>(null)
const danmuCWidth = useRef(0) const barrageCWidth = useRef(0)
const timer = useRef(0) const timer = useRef(0)
const index = useRef(0) const index = useRef(0)
...@@ -37,14 +37,14 @@ const InternalBarrage: ForwardRefRenderFunction<unknown, Partial<BarrageProps>> ...@@ -37,14 +37,14 @@ const InternalBarrage: ForwardRefRenderFunction<unknown, Partial<BarrageProps>>
useImperativeHandle(ref, () => ({ useImperativeHandle(ref, () => ({
add: (word: string) => { add: (word: string) => {
const _index = index.current % danmu.length const _index = index.current % barrageList.length
danmu.splice(_index, 0, word) barrageList.splice(_index, 0, word)
}, },
})) }))
useEffect(() => { useEffect(() => {
if (dmBody.current) { if (barrageBody.current) {
danmuCWidth.current = dmBody.current.offsetWidth barrageCWidth.current = barrageBody.current.offsetWidth
run() run()
} }
return () => { return () => {
...@@ -61,11 +61,11 @@ const InternalBarrage: ForwardRefRenderFunction<unknown, Partial<BarrageProps>> ...@@ -61,11 +61,11 @@ const InternalBarrage: ForwardRefRenderFunction<unknown, Partial<BarrageProps>>
} }
const play = () => { const play = () => {
const _index = loop ? index.current % danmu.length : index.current const _index = loop ? index.current % barrageList.length : index.current
let el = document.createElement(`div`) let el = document.createElement(`div`)
el.innerHTML = danmu[_index] as string el.innerHTML = barrageList[_index] as string
el.classList.add('dmitem') el.classList.add('barrage-item')
;(dmContainer.current as HTMLDivElement).appendChild(el) ;(barrageContainer.current as HTMLDivElement).appendChild(el)
const width = el.offsetWidth const width = el.offsetWidth
const height = el.offsetHeight const height = el.offsetHeight
...@@ -73,17 +73,17 @@ const InternalBarrage: ForwardRefRenderFunction<unknown, Partial<BarrageProps>> ...@@ -73,17 +73,17 @@ const InternalBarrage: ForwardRefRenderFunction<unknown, Partial<BarrageProps>>
el.style.animationDuration = `${speeds}ms` el.style.animationDuration = `${speeds}ms`
el.style.top = (_index % rows) * (height + top) + 20 + 'px' el.style.top = (_index % rows) * (height + top) + 20 + 'px'
el.style.width = width + 20 + 'px' el.style.width = width + 20 + 'px'
el.style.setProperty('--move-distance', `-${danmuCWidth.current}px`) el.style.setProperty('--move-distance', `-${barrageCWidth.current}px`)
el.dataset.index = `${_index}` el.dataset.index = `${_index}`
el.addEventListener('animationend', () => { el.addEventListener('animationend', () => {
;(dmContainer.current as HTMLDivElement).removeChild(el) ;(barrageContainer.current as HTMLDivElement).removeChild(el)
}) })
index.current++ index.current++
} }
return ( return (
<div className={classes} ref={dmBody} {...restProps}> <div className={classes} ref={barrageBody} {...restProps}>
<div ref={dmContainer} className="dmContainer"></div> <div ref={barrageContainer} className="bContainer"></div>
</div> </div>
) )
} }
......
...@@ -4,17 +4,17 @@ import { Barrage } from './barrage' ...@@ -4,17 +4,17 @@ import { Barrage } from './barrage'
import Button from '@/packages/button' import Button from '@/packages/button'
import './demo.scss' import './demo.scss'
interface danmuRefState { interface barrageRefState {
add: (word: string) => void add: (word: string) => void
} }
const BarrageDemo = () => { const BarrageDemo = () => {
let list = ['画美不看', '不明觉厉', '喜大普奔', '男默女泪', '累觉不爱', '爷青结-'] let list = ['画美不看', '不明觉厉', '喜大普奔', '男默女泪', '累觉不爱', '爷青结-']
const danmuRef = useRef<danmuRefState>(null) const barrageRef = useRef<barrageRefState>(null)
const addDanmu = () => { const addBarrage = () => {
let n = Math.random() let n = Math.random()
if (danmuRef.current) { if (barrageRef.current) {
danmuRef.current.add('随机——' + String(n).substr(2, 10)) barrageRef.current.add('随机——' + String(n).substr(2, 10))
} }
} }
return ( return (
...@@ -22,10 +22,10 @@ const BarrageDemo = () => { ...@@ -22,10 +22,10 @@ const BarrageDemo = () => {
<div className="demo"> <div className="demo">
<h2>基础用法</h2> <h2>基础用法</h2>
<Cell> <Cell>
<Barrage ref={danmuRef} danmu={list}></Barrage> <Barrage ref={barrageRef} barrageList={list}></Barrage>
</Cell> </Cell>
<div className="test" style={{ textAlign: 'center' }}> <div className="test" style={{ textAlign: 'center' }}>
<Button type="danger" onClick={addDanmu}> <Button type="danger" onClick={addBarrage}>
随机添加 随机添加
</Button> </Button>
</div> </div>
......
...@@ -17,20 +17,20 @@ import { Barrage } from '@nutui/nutui-react'; ...@@ -17,20 +17,20 @@ import { Barrage } from '@nutui/nutui-react';
```tsx ```tsx
<h2>基础用法</h2> <h2>基础用法</h2>
<Cell> <Cell>
<Barrage ref={danmuRef} danmu={list}></Barrage> <Barrage ref={barrageRef} barrageList={list}></Barrage>
</Cell> </Cell>
<div className="test" style={{ textAlign: 'center' }}> <div className="test" style={{ textAlign: 'center' }}>
<Button type="danger" onClick={addDanmu}>随机添加</Button> <Button type="danger" onClick={addBarrage}>随机添加</Button>
</div> </div>
``` ```
```tsx ```tsx
let list = ['画美不看', '不明觉厉', '喜大普奔', '男默女泪', '累觉不爱', '爷青结-'] let list = ['画美不看', '不明觉厉', '喜大普奔', '男默女泪', '累觉不爱', '爷青结-']
const danmuRef = useRef<danmuRefState>(null) const barrageRef = useRef(null)
const addDanmu = () => { const addBarrage = () => {
let n = Math.random() let n = Math.random()
if (danmuRef.current) { if (barrageRef.current) {
danmuRef.current.add('随机——' + String(n).substr(2, 10)) barrageRef.current.add('随机——' + String(n).substr(2, 10))
} }
} }
``` ```
...@@ -43,7 +43,7 @@ const addDanmu = () => { ...@@ -43,7 +43,7 @@ const addDanmu = () => {
| 参数 | 说明 | 类型 | 默认值 | | 参数 | 说明 | 类型 | 默认值 |
|--------------|----------------------------------|--------|------------------| |--------------|----------------------------------|--------|------------------|
| danmu | 弹幕列表数据 | Array | [] | | barrageList | 弹幕列表数据 | Array | [] |
| frequency | 可视区域内每个弹幕出现的时间间隔 | Number | 500 | | frequency | 可视区域内每个弹幕出现的时间间隔 | Number | 500 |
| speeds | 每个弹幕的滚动时间 | Number | 2000 | | speeds | 每个弹幕的滚动时间 | Number | 2000 |
| rows | 弹幕行数,分几行展示 | Number | 1 | | rows | 弹幕行数,分几行展示 | Number | 1 |
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册