You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design/components/affix/index.jsx

110 lines
2.4 KiB
React

10 years ago
import React from 'react';
import ReactDOM from 'react-dom';
import { Dom } from 'rc-util';
import classNames from 'classnames';
10 years ago
function getScroll(w, top) {
let ret = w[`page${top ? 'Y' : 'X'}Offset`];
const method = `scroll${top ? 'Top' : 'Left'}`;
if (typeof ret !== 'number') {
const 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) {
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);
return {
top: rect.top + scrollTop - clientTop,
left: rect.left + scrollLeft - clientLeft,
};
}
const Affix = React.createClass({
10 years ago
getDefaultProps() {
return {
offset: 0,
10 years ago
};
},
propTypes: {
offset: React.PropTypes.number,
},
10 years ago
getInitialState() {
return {
affix: false,
affixStyle: null,
10 years ago
};
},
handleScroll() {
const affix = this.state.affix;
const scrollTop = getScroll(window, true);
const elemOffset = getOffset(ReactDOM.findDOMNode(this));
10 years ago
if (!affix && (elemOffset.top - this.props.offset) < scrollTop) {
10 years ago
this.setState({
affix: true,
affixStyle: {
top: this.props.offset,
left: elemOffset.left,
width: ReactDOM.findDOMNode(this).offsetWidth,
},
10 years ago
});
}
if (affix && (elemOffset.top - this.props.offset) > scrollTop) {
10 years ago
this.setState({
affix: false,
affixStyle: null,
10 years ago
});
}
},
componentDidMount() {
this.scrollEvent = Dom.addEventListener(window, 'scroll', this.handleScroll);
this.resizeEvent = Dom.addEventListener(window, 'resize', this.handleScroll);
10 years ago
},
componentWillUnmount() {
if (this.scrollEvent) {
this.scrollEvent.remove();
}
if (this.resizeEvent) {
this.resizeEvent.remove();
}
10 years ago
},
render() {
const className = classNames({
[this.props.className]: this.props.className,
'ant-affix': this.state.affix,
});
10 years ago
return (
<div {...this.props}>
<div className={className} style={this.state.affixStyle}>
{this.props.children}
</div>
10 years ago
</div>
);
},
10 years ago
});
module.exports = Affix;