Skip to content

Instantly share code, notes, and snippets.

@enqtran
Last active May 25, 2022 19:16
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save enqtran/25c6b222a73dc497cc3a64c090fb6700 to your computer and use it in GitHub Desktop.
Save enqtran/25c6b222a73dc497cc3a64c090fb6700 to your computer and use it in GitHub Desktop.
[ReactJS] Detect Scrolls To Bottom
constructor(props) {
super(props);
this.state = {
height: window.innerHeight,
message: 'not at bottom'
};
this.handleScroll = this.handleScroll.bind(this);
}
handleScroll() {
const windowHeight = "innerHeight" in window ? window.innerHeight : document.documentElement.offsetHeight;
const body = document.body;
const html = document.documentElement;
const docHeight = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
const windowBottom = windowHeight + window.pageYOffset;
if (windowBottom >= docHeight) {
this.setState({
message: 'bottom reached'
});
} else {
this.setState({
message: 'not at bottom'
});
}
}
componentDidMount() {
window.addEventListener("scroll", this.handleScroll);
}
componentWillUnmount() {
window.removeEventListener("scroll", this.handleScroll);
}
@TendiF
Copy link

TendiF commented Nov 10, 2020

love it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment