mirror of
https://github.com/peter-tanner/peter-tanner.github.io.git
synced 2024-11-30 20:10:18 +08:00
20 lines
415 B
JavaScript
20 lines
415 B
JavaScript
/**
|
|
* Reference: https://bootsnipp.com/snippets/featured/link-to-top-page
|
|
*/
|
|
|
|
export function back2top() {
|
|
const btn = document.getElementById('back-to-top');
|
|
|
|
window.addEventListener('scroll', () => {
|
|
if (window.scrollY > 50) {
|
|
btn.classList.add('show');
|
|
} else {
|
|
btn.classList.remove('show');
|
|
}
|
|
});
|
|
|
|
btn.addEventListener('click', () => {
|
|
window.scrollTo({ top: 0 });
|
|
});
|
|
}
|