Last active
February 26, 2016 16:32
-
-
Save adrianmcli/1df30cc9e62fdaec00da to your computer and use it in GitHub Desktop.
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
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