Smooth scroll to anchor without a library

Custom function using scrollIntoView with smooth behavior
This commit is contained in:
2020-04-07 18:47:02 +02:00
parent 1a05c35fc8
commit c5836dcbc1
4 changed files with 16 additions and 13 deletions

View File

@@ -179,6 +179,19 @@ export const parallaxAnime = (element, anime) => {
}
/*
** Smooth scroll to anchor on click
*/
export const smoothScroll = event => {
const link = event.target.closest('a')
const hash = link.getAttribute('href').split('#')[1]
const target = document.getElementById(hash)
target.scrollIntoView({ behavior: 'smooth' })
history.pushState({}, null, `#${hash}`)
event.preventDefault()
}
/*
** Google Analytics send page
*/