Skip to content

Instantly share code, notes, and snippets.

@daniilgrigoryev
Created July 10, 2019 10:09
Show Gist options
  • Select an option

  • Save daniilgrigoryev/83cdd1216c04aa1cc21469d6d02cdfe4 to your computer and use it in GitHub Desktop.

Select an option

Save daniilgrigoryev/83cdd1216c04aa1cc21469d6d02cdfe4 to your computer and use it in GitHub Desktop.
Handle Scroll React
class SidePanel extends Component{
constructor(props) {
super(props);
this.handleScroll = this.handleScroll.bind(this);
}
componentDidMount() {
window.addEventListener('scroll', this.handleScroll, true);
}
componentWillUnmount(){
window.removeEventListener('scroll', this.handleScroll, false);
}
handleScroll(){
const elements = document.querySelectorAll('div[scrollanchor]');
elements.forEach((el, i) => {
const elTop = el.getBoundingClientRect().top;
const elBottom = el.getBoundingClientRect().bottom;
if (elTop >= 120 || elBottom <= 120) {
el.classList.remove('active');
}else if (elTop <= 120 && elBottom >= 120) {
el.classList.add('active');
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment