-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.jsx
101 lines (96 loc) · 2.97 KB
/
index.jsx
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
"use strict";
var React = require('react');
var PropTypes = require('prop-types');
var createReactClass = require('create-react-class');
var ReactDOM = require('react-dom');
var util = require('dom-find');
var StickyDiv = createReactClass({
displayName:"StickyDiv",
propTypes: {
offsetTop: PropTypes.number,
zIndex: PropTypes.number,
className: PropTypes.string
},
getDefaultProps: function() {
return {
offsetTop: 0,
className: '',
zIndex: 9999
};
},
getInitialState : function(){
return {
fix: false,
width: null
};
},
handleResize : function(){
this.checkWidth();
this.checkPositions();
},
onScroll: function () {
this.checkWidth();
this.checkPositions();
},
checkPositions: function(){
var pos = util.findPosRelativeToViewport(ReactDOM.findDOMNode(this));
if (pos[1]<=this.props.offsetTop){
this.setState({fix: true});
} else {
this.setState({fix: false});
}
},
checkWidth: function () {
var width = null;
if (this.refs.duplicate) {
width = this.refs.duplicate.getBoundingClientRect().width;
} else {
width = this.refs.original.getBoundingClientRect().width;
}
if (this.state.width !== width) {
this.setState({
width: width
});
}
},
componentDidMount: function () {
window.addEventListener('scroll', this.onScroll, false);
window.addEventListener('resize', this.handleResize);
this.checkWidth();
},
componentWillUnmount: function () {
window.removeEventListener('scroll', this.onScroll, false);
window.removeEventListener('resize', this.handleResize);
},
render: function () {
var divStyle;
if (this.state.fix) {
divStyle = {
display: 'block',
position: 'fixed',
width: this.state.width ? (this.state.width + 'px') : null,
top: this.props.offsetTop
};
return <div style={{zIndex : this.props.zIndex, position:'relative', width:'100%'}}>
<div ref='duplicate' key='duplicate' style={{visibility:'hidden'}}>
{this.props.children}
</div>
<div ref='original' key='original' className={this.props.className} style={divStyle} >
{this.props.children}
</div>
</div>;
}
else {
divStyle = {
display: 'block',
position: 'relative'
};
return <div style={{zIndex : this.props.zIndex, position:'relative', width:'100%'}}>
<div ref='original' key='original' style={divStyle}>
{this.props.children}
</div>
</div>;
}
}
});
module.exports = StickyDiv;