forked from ant-design/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# 基本 | ||
|
||
- order: 0 | ||
|
||
最简单的用法。 | ||
|
||
--- | ||
|
||
````jsx | ||
var Affix = antd.Affix; | ||
|
||
React.render( | ||
<Affix> | ||
<button className="ant-btn ant-btn-primary">offset=0</button> | ||
</Affix> | ||
, document.getElementById('components-affix-demo-basic')); | ||
```` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# 偏移 | ||
|
||
- order: 1 | ||
|
||
达到一定的偏移量才触发。 | ||
|
||
--- | ||
|
||
````jsx | ||
var Affix = antd.Affix; | ||
|
||
React.render( | ||
<Affix offset="200"> | ||
<button className="ant-btn ant-btn-primary">offset=200</button> | ||
</Affix> | ||
, document.getElementById('components-affix-demo-offset')); | ||
```` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React from 'react'; | ||
import joinClasses from 'react/lib/joinClasses'; | ||
import rcUtil from 'rc-util'; | ||
|
||
var Affix = React.createClass({ | ||
|
||
getDefaultProps() { | ||
return { | ||
offset: 0 | ||
}; | ||
}, | ||
|
||
getInitialState() { | ||
return { | ||
affix: false | ||
}; | ||
}, | ||
|
||
handleScroll() { | ||
var affix = this.state.affix; | ||
var offset = this.props.offset; | ||
var scrollTop = (document.documentElement && document.documentElement.scrollTop) || | ||
document.body.scrollTop; | ||
|
||
if (!affix && scrollTop >= offset) { | ||
this.setState({ | ||
affix: true | ||
}); | ||
} | ||
|
||
if (affix && scrollTop < offset) { | ||
this.setState({ | ||
affix: false | ||
}); | ||
} | ||
}, | ||
|
||
componentDidMount() { | ||
this.scrollEvent = rcUtil.Dom.addEventListener(window, 'scroll', this.handleScroll); | ||
}, | ||
|
||
componentWillUnmount() { | ||
if (this.scrollEvent) { | ||
this.scrollEvent.remove(); | ||
} | ||
}, | ||
|
||
render() { | ||
var affix = this.state.affix ? 'affix' : ''; | ||
var className = this.props.className; | ||
|
||
return ( | ||
<div {...this.props} className={joinClasses(className, affix)}> | ||
{this.props.children} | ||
</div> | ||
); | ||
} | ||
|
||
}); | ||
|
||
module.exports = Affix; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters