From 37e376df1128a554acc71d277a3053ba9b360b8e Mon Sep 17 00:00:00 2001 From: ddcat1115 Date: Thu, 26 May 2016 16:06:15 +0800 Subject: [PATCH] fix #1777 --- components/affix/demo/on-change.md | 21 +++++++++++++++++++++ components/affix/index.jsx | 11 +++++++---- components/affix/index.md | 1 + 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 components/affix/demo/on-change.md diff --git a/components/affix/demo/on-change.md b/components/affix/demo/on-change.md new file mode 100644 index 0000000000..7d8762f5df --- /dev/null +++ b/components/affix/demo/on-change.md @@ -0,0 +1,21 @@ +--- +order: 3 +title: 固定状态改变的回调 +--- + +可以获得是否固定的状态。 + +````jsx +import { Affix, Button } from 'antd'; + + +const onChange = function (affixed) { + console.log(affixed); // true or false +}; + +ReactDOM.render( + + + +, mountNode); +```` diff --git a/components/affix/index.jsx b/components/affix/index.jsx index 39f575c04c..c0b693ee4b 100644 --- a/components/affix/index.jsx +++ b/components/affix/index.jsx @@ -39,6 +39,10 @@ export default class Affix extends React.Component { offsetBottom: React.PropTypes.number, } + static defaultProps = { + onChange() {}, + } + constructor(props) { super(props); this.state = { @@ -51,7 +55,6 @@ export default class Affix extends React.Component { // Backwards support offsetTop = offsetTop || offset; - const scrollTop = getScroll(window, true); const elemOffset = getOffset(ReactDOM.findDOMNode(this)); const elemSize = { @@ -78,7 +81,7 @@ export default class Affix extends React.Component { left: elemOffset.left, width: ReactDOM.findDOMNode(this).offsetWidth, }, - }); + }, () => this.props.onChange(!!this.state.affixStyle)); } } else if (scrollTop < elemOffset.top + elemSize.height + offsetBottom - window.innerHeight && offsetMode.bottom) { @@ -91,12 +94,12 @@ export default class Affix extends React.Component { left: elemOffset.left, width: ReactDOM.findDOMNode(this).offsetWidth, }, - }); + }, () => this.props.onChange(!!this.state.affixStyle)); } } else if (this.state.affixStyle) { this.setState({ affixStyle: null, - }); + }, () => this.props.onChange(!!this.state.affixStyle)); } } diff --git a/components/affix/index.md b/components/affix/index.md index c602f3dc68..c8293cbab9 100644 --- a/components/affix/index.md +++ b/components/affix/index.md @@ -20,3 +20,4 @@ english: Affix |-------------|----------------|--------------------|--------------| | offsetTop | 距离窗口顶部达到指定偏移量后触发 | Number | | | offsetBottom | 距离窗口底部达到指定偏移量后触发 | Number | | +| onChange | 固定状态改变时触发的回调函数 | Function | 无 |