A Pen by Hirokazu Takatama on CodePen.
Created
January 5, 2018 11:07
-
-
Save xsaamiir/796a0889e57da36743b069f5b8a48d5f to your computer and use it in GitHub Desktop.
React: scrollIntoView
This file contains hidden or 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
| <div id="list"></div> |
This file contains hidden or 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
| var items = []; | |
| for (var i = 0; i < 100; i++) { | |
| items.push(i); | |
| } | |
| var List = React.createClass({ | |
| getInitialState: function() { | |
| return {index: 0}; | |
| }, | |
| handleShow: function(i) { | |
| this.setState({index: i}); | |
| this.refs[i].scrollIntoView({block: 'end', behavior: 'smooth'}); | |
| }, | |
| render: function() { | |
| return( | |
| <div> | |
| <ul>{items.map(function(item, i) {return <li ref={i}>{item}</li>})}</ul> | |
| <button onClick={this.handleShow.bind(this, 0)}>0</button> | |
| <button onClick={this.handleShow.bind(this, 50)}>50</button> | |
| <button onClick={this.handleShow.bind(this, 99)}>99</button> | |
| {this.state.index} | |
| </div> | |
| ); | |
| } | |
| }); | |
| React.render(<List />, document.getElementById('list')); |
This file contains hidden or 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
| <script src="//cdnjs.cloudflare.com/ajax/libs/react/0.14.3/react.min.js"></script> |
This file contains hidden or 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
| #list ul { | |
| overflow: auto; | |
| height: 128px; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment