ant-design/components/affix/index.jsx

110 lines
2.4 KiB
React
Raw Normal View History

2015-08-03 16:49:42 +08:00
import React from 'react';
2015-10-21 17:59:57 +08:00
import ReactDOM from 'react-dom';
2015-08-03 16:49:42 +08:00
import rcUtil from 'rc-util';
import classNames from 'classnames';
2015-08-03 16:49:42 +08:00
2015-08-04 14:57:18 +08:00
function getScroll(w, top) {
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;
// ie6,7,8 standard mode
2015-08-04 14:57:18 +08:00
ret = d.documentElement[method];
if (typeof ret !== 'number') {
// quirks mode
2015-08-04 14:57:18 +08:00
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);
2015-10-21 17:59:57 +08:00
let elemOffset = getOffset(ReactDOM.findDOMNode(this));
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,
2015-10-21 17:59:57 +08:00
width: ReactDOM.findDOMNode(this).offsetWidth
2015-08-04 14:57:18 +08:00
}
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() {
const className = classNames({
[this.props.className]: this.props.className,
'ant-affix': this.state.affix
});
2015-08-03 16:49:42 +08:00
return (
2015-08-04 14:57:18 +08:00
<div {...this.props}>
<div className={className} style={this.state.affixStyle}>
2015-08-04 14:57:18 +08:00
{this.props.children}
</div>
2015-08-03 16:49:42 +08:00
</div>
);
}
});
module.exports = Affix;