ant-design/components/affix/index.jsx

107 lines
2.4 KiB
React
Raw Normal View History

2015-08-03 16:49:42 +08:00
import React from 'react';
import joinClasses from 'react/lib/joinClasses';
import rcUtil from 'rc-util';
2015-08-04 14:57:18 +08:00
function getScroll(w, top) {
2015-09-01 16:18:46 +08:00
let ret = w['page' + (top ? 'Y' : 'X') + 'Offset'];
let method = 'scroll' + (top ? 'Top' : 'Left');
2015-08-04 14:57:18 +08:00
if (typeof ret !== 'number') {
2015-09-01 16:18:46 +08:00
let d = w.document;
2015-08-04 14:57:18 +08:00
//ie6,7,8 standard mode
ret = d.documentElement[method];
if (typeof ret !== 'number') {
//quirks mode
ret = d.body[method];
}
}
return ret;
}
function getOffset(element) {
2015-09-01 16:18:46 +08:00
let rect = element.getBoundingClientRect();
let body = document.body;
let clientTop = element.clientTop || body.clientTop || 0;
let clientLeft = element.clientLeft || body.clientLeft || 0;
let scrollTop = getScroll(window, true);
let scrollLeft = getScroll(window);
2015-08-04 14:57:18 +08:00
return {
top: rect.top + scrollTop - clientTop,
left: rect.left + scrollLeft - clientLeft
};
}
2015-09-01 16:18:46 +08:00
let Affix = React.createClass({
2015-08-03 16:49:42 +08:00
getDefaultProps() {
return {
offset: 0
};
},
2015-09-01 16:18:46 +08:00
propTypes: {
offset: React.PropTypes.number
},
2015-08-03 16:49:42 +08:00
getInitialState() {
return {
2015-08-04 14:57:18 +08:00
affix: false,
affixStyle: null
2015-08-03 16:49:42 +08:00
};
},
handleScroll() {
2015-09-01 16:18:46 +08:00
let affix = this.state.affix;
let scrollTop = getScroll(window, true);
let elemOffset = getOffset(this.getDOMNode());
2015-08-03 16:49:42 +08:00
2015-08-04 14:57:18 +08:00
if (!affix && (elemOffset.top - this.props.offset) < scrollTop) {
2015-08-03 16:49:42 +08:00
this.setState({
2015-08-04 14:57:18 +08:00
affix: true,
affixStyle: {
top: this.props.offset,
left: elemOffset.left,
width: this.getDOMNode().offsetWidth
}
2015-08-03 16:49:42 +08:00
});
}
2015-08-04 14:57:18 +08:00
if (affix && (elemOffset.top - this.props.offset) > scrollTop) {
2015-08-03 16:49:42 +08:00
this.setState({
2015-08-04 14:57:18 +08:00
affix: false,
affixStyle: null
2015-08-03 16:49:42 +08:00
});
}
},
componentDidMount() {
this.scrollEvent = rcUtil.Dom.addEventListener(window, 'scroll', this.handleScroll);
2015-08-04 15:03:00 +08:00
this.resizeEvent = rcUtil.Dom.addEventListener(window, 'resize', this.handleScroll);
2015-08-03 16:49:42 +08:00
},
componentWillUnmount() {
if (this.scrollEvent) {
this.scrollEvent.remove();
}
2015-08-04 15:03:00 +08:00
if (this.resizeEvent) {
this.resizeEvent.remove();
}
2015-08-03 16:49:42 +08:00
},
render() {
2015-09-01 16:18:46 +08:00
let affix = this.state.affix ? 'ant-affix' : '';
let className = this.props.className;
2015-08-03 16:49:42 +08:00
return (
2015-08-04 14:57:18 +08:00
<div {...this.props}>
<div className={joinClasses(className, affix)} style={this.state.affixStyle}>
{this.props.children}
</div>
2015-08-03 16:49:42 +08:00
</div>
);
}
});
module.exports = Affix;