Created
February 18, 2019 20:38
-
-
Save Wellers0n/d0b6d8c65085411e68fe5a0fa12c42b2 to your computer and use it in GitHub Desktop.
Scroll smooth
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
class App extends Component { | |
constructor(props){ | |
super(props) | |
this.scrollToIdOnClick = this.scrollToIdOnClick.bind() | |
} | |
scrollToIdOnClick(event){ | |
event.preventDefault() | |
const element = event.target; | |
const id = element.getAttribute('href') | |
const to = document.querySelector(id).offsetTop; | |
window.scroll({ | |
top: to, | |
behavior: "smooth" | |
}) | |
} | |
componentDidMount() { | |
const menuItem = document.querySelectorAll('.containerNav nav a[href^="#"]'); | |
menuItem.forEach(item => { | |
item.addEventListener('click', this.scrollToIdOnClick) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment