mirror of
https://github.com/ant-design/ant-design.git
synced 2025-08-06 16:06:28 +08:00
Improve code for #2247
This commit is contained in:
parent
f687c95bd9
commit
4ac775212f
@ -8,13 +8,8 @@ title: 固定状态改变的回调
|
|||||||
````jsx
|
````jsx
|
||||||
import { Affix, Button } from 'antd';
|
import { Affix, Button } from 'antd';
|
||||||
|
|
||||||
|
|
||||||
const onChange = function (affixed) {
|
|
||||||
console.log(affixed); // true or false
|
|
||||||
};
|
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<Affix offsetTop={120} onChange={onChange}>
|
<Affix offsetTop={120} onChange={affixed => console.log(affixed)}>
|
||||||
<Button>固定在距离顶部 120px 的位置</Button>
|
<Button>固定在距离顶部 120px 的位置</Button>
|
||||||
</Affix>
|
</Affix>
|
||||||
, mountNode);
|
, mountNode);
|
||||||
|
@ -3,6 +3,7 @@ import ReactDOM from 'react-dom';
|
|||||||
import addEventListener from 'rc-util/lib/Dom/addEventListener';
|
import addEventListener from 'rc-util/lib/Dom/addEventListener';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import warning from 'warning';
|
import warning from 'warning';
|
||||||
|
import shallowequal from 'shallowequal';
|
||||||
|
|
||||||
function getScroll(w, top) {
|
function getScroll(w, top) {
|
||||||
let ret = w[`page${top ? 'Y' : 'X'}Offset`];
|
let ret = w[`page${top ? 'Y' : 'X'}Offset`];
|
||||||
@ -50,6 +51,20 @@ export default class Affix extends React.Component {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setAffixStyle(affixStyle) {
|
||||||
|
const originalAffixStyle = this.state.affixStyle;
|
||||||
|
if (shallowequal(affixStyle, originalAffixStyle)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.setState({ affixStyle }, () => {
|
||||||
|
const affixed = !!this.state.affixStyle;
|
||||||
|
if ((affixStyle && !originalAffixStyle) ||
|
||||||
|
(!affixStyle && originalAffixStyle)) {
|
||||||
|
this.props.onChange(affixed);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
handleScroll = () => {
|
handleScroll = () => {
|
||||||
let { offsetTop, offsetBottom, offset } = this.props;
|
let { offsetTop, offsetBottom, offset } = this.props;
|
||||||
|
|
||||||
@ -73,29 +88,23 @@ export default class Affix extends React.Component {
|
|||||||
|
|
||||||
if (scrollTop > elemOffset.top - offsetTop && offsetMode.top) {
|
if (scrollTop > elemOffset.top - offsetTop && offsetMode.top) {
|
||||||
// Fixed Top
|
// Fixed Top
|
||||||
this.setState({
|
this.setAffixStyle({
|
||||||
affixStyle: {
|
position: 'fixed',
|
||||||
position: 'fixed',
|
top: offsetTop,
|
||||||
top: offsetTop,
|
left: elemOffset.left,
|
||||||
left: elemOffset.left,
|
width: ReactDOM.findDOMNode(this).offsetWidth,
|
||||||
width: ReactDOM.findDOMNode(this).offsetWidth,
|
});
|
||||||
},
|
|
||||||
}, () => { if (!this.state.affixStyle) this.props.onChange(!!this.state.affixStyle) });
|
|
||||||
} else if (scrollTop < elemOffset.top + elemSize.height + offsetBottom - window.innerHeight &&
|
} else if (scrollTop < elemOffset.top + elemSize.height + offsetBottom - window.innerHeight &&
|
||||||
offsetMode.bottom) {
|
offsetMode.bottom) {
|
||||||
// Fixed Bottom
|
// Fixed Bottom
|
||||||
this.setState({
|
this.setAffixStyle({
|
||||||
affixStyle: {
|
position: 'fixed',
|
||||||
position: 'fixed',
|
bottom: offsetBottom,
|
||||||
bottom: offsetBottom,
|
left: elemOffset.left,
|
||||||
left: elemOffset.left,
|
width: ReactDOM.findDOMNode(this).offsetWidth,
|
||||||
width: ReactDOM.findDOMNode(this).offsetWidth,
|
});
|
||||||
},
|
} else {
|
||||||
}, () => { if (!this.state.affixStyle) this.props.onChange(!!this.state.affixStyle) });
|
this.setAffixStyle(null);
|
||||||
} else if (this.state.affixStyle) {
|
|
||||||
this.setState({
|
|
||||||
affixStyle: null,
|
|
||||||
}, () => this.props.onChange(!!this.state.affixStyle));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,6 +70,7 @@
|
|||||||
"rc-util": "~3.1.0",
|
"rc-util": "~3.1.0",
|
||||||
"react-addons-pure-render-mixin": "^15.0.0",
|
"react-addons-pure-render-mixin": "^15.0.0",
|
||||||
"react-slick": "~0.12.0",
|
"react-slick": "~0.12.0",
|
||||||
|
"shallowequal": "^0.2.2",
|
||||||
"warning": "~3.0.0"
|
"warning": "~3.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
Loading…
Reference in New Issue
Block a user