Files
andwati.com/static/js/scroll-top.js
T
2026-04-02 13:14:18 +03:00

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;
}
});
});