Skip to content

Instantly share code, notes, and snippets.

@adrianmcli
Last active February 26, 2016 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianmcli/1df30cc9e62fdaec00da to your computer and use it in GitHub Desktop.
Save adrianmcli/1df30cc9e62fdaec00da to your computer and use it in GitHub Desktop.
componentWillReceiveProps() {
// before the new props come in, save the current scroll position
const ele = $(this._container);
const distanceFromBottom = ele[0].scrollHeight - ele.scrollTop() - ele.outerHeight();
this.setState({
distanceFromBottom,
});
}
componentDidUpdate() {
const ele = $(this._container);
// setting scrollTop is how we can programatically change the scroll position of a div
// in order to find out what value to use, we rearrange the above equation to isolate for scrollTop
const targetScrollTopValue = ele[0].scrollHeight - ele.outerHeight() - this.state.distanceFromBottom;
ele.scrollTop(targetScrollTopValue); // set the scrollTop value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment