ant-design/components/affix/index.jsx

127 lines
3.2 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';
2016-02-29 12:10:36 +08:00
import { Dom } 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`];
2016-02-29 12:10:36 +08:00
const method = `scroll${top ? 'Top' : 'Left'}`;
2015-08-04 14:57:18 +08:00
if (typeof ret !== 'number') {
2016-02-29 12:10:36 +08:00
const 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) {
2016-02-29 12:10:36 +08:00
const rect = element.getBoundingClientRect();
const body = document.body;
const clientTop = element.clientTop || body.clientTop || 0;
const clientLeft = element.clientLeft || body.clientLeft || 0;
const scrollTop = getScroll(window, true);
const scrollLeft = getScroll(window);
2015-08-04 14:57:18 +08:00
return {
top: rect.top + scrollTop - clientTop,
2016-02-29 12:10:36 +08:00
left: rect.left + scrollLeft - clientLeft,
2015-08-04 14:57:18 +08:00
};
}
2016-02-29 12:10:36 +08:00
const Affix = React.createClass({
2015-09-01 16:18:46 +08:00
propTypes: {
2016-02-29 16:44:38 +08:00
offsetTop: React.PropTypes.number,
offsetBottom: React.PropTypes.number,
2015-09-01 16:18:46 +08:00
},
2015-08-03 16:49:42 +08:00
getInitialState() {
return {
2016-02-29 12:10:36 +08:00
affixStyle: null,
2015-08-03 16:49:42 +08:00
};
},
handleScroll() {
2016-02-29 16:44:38 +08:00
let { offsetTop, offsetBottom } = this.props;
2016-02-29 12:10:36 +08:00
const scrollTop = getScroll(window, true);
const elemOffset = getOffset(ReactDOM.findDOMNode(this));
2016-02-29 16:44:38 +08:00
const elemSize = {
width: ReactDOM.findDOMNode(this.refs.fixedNode).offsetWidth,
height: ReactDOM.findDOMNode(this.refs.fixedNode).offsetHeight,
};
2015-08-03 16:49:42 +08:00
2016-02-29 16:44:38 +08:00
const offsetMode = {};
if (typeof offsetTop !== 'number' && typeof offsetBottom !== 'number') {
offsetMode.top = true;
offsetTop = 0;
} else {
offsetMode.top = typeof offsetTop === 'number';
offsetMode.bottom = typeof offsetBottom === 'number';
2015-08-03 16:49:42 +08:00
}
2016-02-29 16:44:38 +08:00
if (scrollTop > elemOffset.top - offsetTop && offsetMode.top) {
// Fixed Top
if (!this.state.affixStyle) {
this.setState({
affixStyle: {
position: 'fixed',
top: offsetTop,
left: elemOffset.left,
},
});
}
} else if (scrollTop < elemOffset.top + elemSize.height + offsetBottom - window.innerHeight &&
offsetMode.bottom) {
// Fixed Bottom
if (!this.state.affixStyle) {
this.setState({
affixStyle: {
position: 'fixed',
bottom: offsetBottom,
left: elemOffset.left,
},
});
}
} else if (this.state.affixStyle) {
2015-08-03 16:49:42 +08:00
this.setState({
2016-02-29 12:10:36 +08:00
affixStyle: null,
2015-08-03 16:49:42 +08:00
});
}
},
componentDidMount() {
2016-02-29 12:10:36 +08:00
this.scrollEvent = Dom.addEventListener(window, 'scroll', this.handleScroll);
this.resizeEvent = 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,
2016-02-29 16:44:38 +08:00
'ant-affix': this.state.affixStyle,
});
2015-08-03 16:49:42 +08:00
return (
2015-08-04 14:57:18 +08:00
<div {...this.props}>
2016-02-29 16:44:38 +08:00
<div className={className} ref="fixedNode" 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>
);
2016-02-29 12:10:36 +08:00
},
2015-08-03 16:49:42 +08:00
});
export default Affix;