Created
July 10, 2019 10:09
-
-
Save daniilgrigoryev/83cdd1216c04aa1cc21469d6d02cdfe4 to your computer and use it in GitHub Desktop.
Handle Scroll React
This file contains hidden or 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
| 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