1
0
mirror of synced 2025-12-19 18:10:59 -05:00
Files
docs/javascripts/scroll-up.js
2021-02-23 08:55:01 -08:00

23 lines
607 B
JavaScript

export default function () {
// function to scroll up to page top
const PageTopBtn = document.getElementById('js-scroll-top')
if (!PageTopBtn) return
PageTopBtn.addEventListener('click', (e) => {
window.scrollTo({
top: 0,
behavior: 'smooth'
})
})
// show scroll button only when display is scroll down
window.addEventListener('scroll', function () {
const y = document.documentElement.scrollTop // get the height from page top
if (y < 100) {
PageTopBtn.classList.remove('show')
} else if (y >= 100) {
PageTopBtn.classList.add('show')
}
})
}