Skip to content

Instantly share code, notes, and snippets.

@Wellers0n
Created February 18, 2019 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wellers0n/d0b6d8c65085411e68fe5a0fa12c42b2 to your computer and use it in GitHub Desktop.
Save Wellers0n/d0b6d8c65085411e68fe5a0fa12c42b2 to your computer and use it in GitHub Desktop.
Scroll smooth
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