Last active
October 4, 2018 02:57
-
-
Save monaye/3e941e69c4521c80f7678bc7e91017e1 to your computer and use it in GitHub Desktop.
Add windows ScollTo event to all class
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
| const targets = document.getElementsByClassName('smoothScroll'); | |
| const scrollToElement = function(event) { | |
| event.preventDefault(); | |
| let elementId = event.target.attributes.href.value.replace('#', ''); | |
| let scrollPosition = document.getElementById(elementId).offsetTop; | |
| window.scrollTo({ | |
| top: scrollPosition, | |
| behavior: "smooth" | |
| }); | |
| } | |
| for(let i = 0; i < targets.length; i++){ | |
| targets[i].addEventListener('click', scrollToElement, false); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment