ant-design/components/affix/index.jsx

103 lines
2.3 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) {
var ret = w['page' + (top ? 'Y' : 'X') + 'Offset'];
var method = 'scroll' + (top ? 'Top' : 'Left');
if (typeof ret !== 'number') {
var d = w.document;
//ie6,7,8 standard mode
ret = d.documentElement[method];
if (typeof ret !== 'number') {
//quirks mode
ret = d.body[method];
}
}
return ret;
}
function getOffset(element) {
var rect = element.getBoundingClientRect();
var body = document.body;
var clientTop = element.clientTop || body.clientTop || 0;
var clientLeft = element.clientLeft || body.clientLeft || 0;
var scrollTop = getScroll(window, true);
var scrollLeft = getScroll(window);
return {
top: rect.top + scrollTop - clientTop,
left: rect.left + scrollLeft - clientLeft
};
}
2015-08-03 16:49:42 +08:00
var Affix = React.createClass({
getDefaultProps() {
return {
offset: 0
};
},
getInitialState() {
return {
2015-08-04 14:57:18 +08:00
affix: false,
affixStyle: null
2015-08-03 16:49:42 +08:00
};
},
handleScroll() {
var affix = this.state.affix;
2015-08-04 14:57:18 +08:00
var scrollTop = getScroll(window, true);
var 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() {
var affix = this.state.affix ? 'affix' : '';
var className = this.props.className;
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;