import * as React from 'react'; import * as Styled from './Marquee.style' /** * 滚动的文字公告 */ export default class Marquee extends React.Component { static defaultProps = { speed: 2, textList: [] } public constructor() { super(); } public componentDidMount() { let step = () => { this.scroll(); this.timer = requestAnimationFrame(step) } this.timer = requestAnimationFrame(step); // setInterval(() => {this.scroll()}, this.props.speed); } public componentWillUnmount() { this.timer && cancelAnimationFrame(this.timer); } public scroll(){ if(this.scrollEnd.offsetWidth - this.scrollWrap.scrollLeft <= 0) { this.scrollWrap.scrollLeft -= this.scrollBegin.offsetWidth; } else { this.scrollWrap.scrollLeft += this.props.speed; } } public render() { const {textList} = this.props; return ( this.scrollWrap = ref}> ) } }