index-main/assets/js/back-to-top.js
Julie Blanc c59f4d93dd
All checks were successful
Deploy / Deploy to Production (push) Successful in 13s
btn back to top
2026-02-23 18:12:04 +01:00

24 lines
No EOL
595 B
JavaScript

export function backToTop(){
const btn = document.getElementById('btn--back-to-top');
if (!btn) return;
let lastY = window.scrollY;
let peakY = window.scrollY;
window.addEventListener('scroll', () => {
const currentY = window.scrollY;
if (currentY > lastY) {
peakY = currentY;
btn.classList.remove('is-visible');
} else {
if (currentY <= 800) {
btn.classList.remove('is-visible');
} else if (peakY - currentY >= 200 && peakY > 600) {
btn.classList.add('is-visible');
}
}
lastY = currentY;
}, { passive: true });
}