Skip to content

Instantly share code, notes, and snippets.

@xsaamiir
Created January 5, 2018 11:07
Show Gist options
  • Select an option

  • Save xsaamiir/796a0889e57da36743b069f5b8a48d5f to your computer and use it in GitHub Desktop.

Select an option

Save xsaamiir/796a0889e57da36743b069f5b8a48d5f to your computer and use it in GitHub Desktop.
React: scrollIntoView
<div id="list"></div>
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'));
<script src="//cdnjs.cloudflare.com/ajax/libs/react/0.14.3/react.min.js"></script>
#list ul {
overflow: auto;
height: 128px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment