mirror of
https://github.com/andwati/andwati.com.git
synced 2026-10-18 09:22:53 +00:00
35 lines
858 B
JavaScript
35 lines
858 B
JavaScript
document.addEventListener('DOMContentLoaded', () => {
|
|
const scrollToTopBtn = document.getElementById('scroll-to-top');
|
|
|
|
if (!scrollToTopBtn) return;
|
|
|
|
// Show button when scrolled down 400px
|
|
const toggleVisibility = () => {
|
|
if (window.scrollY > 400) {
|
|
scrollToTopBtn.classList.add('visible');
|
|
} else {
|
|
scrollToTopBtn.classList.remove('visible');
|
|
}
|
|
};
|
|
|
|
// Smooth scroll to top when clicked
|
|
scrollToTopBtn.addEventListener('click', () => {
|
|
window.scrollTo({
|
|
top: 0,
|
|
behavior: 'smooth',
|
|
});
|
|
});
|
|
|
|
// Throttle the scroll event listener a tiny bit for performance
|
|
let isTicking = false;
|
|
window.addEventListener('scroll', () => {
|
|
if (!isTicking) {
|
|
window.requestAnimationFrame(() => {
|
|
toggleVisibility();
|
|
isTicking = false;
|
|
});
|
|
isTicking = true;
|
|
}
|
|
});
|
|
});
|