import React from 'react';

const ref = React.createRef();
/**
 * @刘丹: 向上滚动
 */
class ScrollTop extends React.Component {
  constructor() {
    super();
    this.state = {
      isScroll: false,
      height: '',
    };
  }
  componentDidMount() {
    if (ref.current) {
      this.initHeight();
    }
  }
  componentDidUpdate(prev) {
    if (prev.text !== this.props.text && ref.current) {
      this.initHeight();
    }   
  }
  initHeight = () => {
    const scrolH = ref.current.parentNode.parentNode.offsetHeight;
    const curH = ref.current.offsetHeight;
    this.setState({
      height: (curH - scrolH  + 10) + 'px',
      isScroll: curH > scrolH,
    });
  };
  render() {
    return (
      <div className='marqueeTop-parent' style={{height: this.state.height}}>
        <div ref={ ref }  className={this.state.isScroll
        ? this.props.isFast ? 'marqueeTop fast' : 'marqueeTop'
        : ''}>
          {this.props.children}
        </div>
      </div>
     
    );
  }
}

export default ScrollTop;
