Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. class App extends Component {
  2. constructor(props){
  3. super(props)
  4.  
  5. this.scrollToIdOnClick = this.scrollToIdOnClick.bind()
  6. }
  7.  
  8. scrollToIdOnClick(event){
  9. event.preventDefault()
  10. const element = event.target;
  11. const id = element.getAttribute('href')
  12. const to = document.querySelector(id).offsetTop;
  13.  
  14. window.scroll({
  15. top: to,
  16. behavior: "smooth"
  17. })
  18. }
  19.  
  20. componentDidMount() {
  21. const menuItem = document.querySelectorAll('.containerNav nav a[href^="#"]');
  22.  
  23. menuItem.forEach(item => {
  24. item.addEventListener('click', this.scrollToIdOnClick)
  25. })
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement