Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
103 lines (87 loc) · 3.01 KB
/
Copy pathindex.js
File metadata and controls
103 lines (87 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import React, { Component } from 'react';
import { render } from 'react-dom';
import PropTypes from 'prop-types';
import './style.less';
class CycleScroll extends Component {
static propTypes = {
content: PropTypes.node.isRequired,
gap: PropTypes.number,
speed: PropTypes.number
}
static defaultProps = {
gap: 0,
speed: 100
}
constructor(props) {
super(props);
}
componentDidMount() {
const {speed} = this.props;
const wrap = this.refs.wrap;
const scrollDiv1 = this.refs.content1;
const scrollDiv2 = this.refs.content2;
wrap.style.height = 145 + 'px'; // 初始值
let MyMar;
function Marquee() {
if (scrollDiv2.offsetHeight === wrap.scrollTop) {
wrap.scrollTop -= scrollDiv1.offsetHeight;
}
wrap.scrollTop++;
}
setTimeout(function() {
// console.log(scrollDiv1.offsetHeight);
// 使用定时器 确保DOM已经加载完毕
wrap.style.height = scrollDiv1.offsetHeight + 'px';
MyMar = setInterval(Marquee, speed);
}, 600);
wrap.onmouseover = function StartScroll() {
clearInterval(MyMar);
};
wrap.onmouseout = function StopScroll() {
MyMar = setInterval(Marquee, speed);
};
}
componentWillReceiveProps(nextProps) {
if (!this.props.content.props.children && nextProps.content.props.children.length > 0) {
const { speed } = this.props;
const wrap = this.refs.wrap;
const scrollDiv1 = this.refs.content1;
const scrollDiv2 = this.refs.content2;
let MyMar;
function Marquee() {
if (scrollDiv2.offsetHeight === wrap.scrollTop) {
wrap.scrollTop -= scrollDiv1.offsetHeight;
}
wrap.scrollTop++;
}
setTimeout(function() {
// console.log(scrollDiv1.offsetHeight);
// 使用定时器 确保DOM已经加载完毕
wrap.style.height = scrollDiv1.offsetHeight + 'px';
MyMar = setInterval(Marquee, speed);
}, 600);
wrap.onmouseover = function StartScroll() {
clearInterval(MyMar);
};
wrap.onmouseout = function StopScroll() {
MyMar = setInterval(Marquee, speed);
};
}
}
render() {
const { content } = this.props;
return (
<div>
<div ref="wrap" className="cycle-wrap">
<div ref="content1" className="cycle-content">
{this.props.content}
</div>
<div ref="content2" className="cycle-content">
{this.props.content}
</div>
</div>
</div>
);
}
}
export default CycleScroll;