Quiero cambiar el tamaño de un encabezado cuando el usuario se desplaza, agregando o restando una clase. Pero, me estoy perdiendo algo. El siguiente script se dispara repetidamente, volviendo a agregar la clase, si un usuario se desplaza lentamente hacia abajo en la página hasta el punto de desplazamiento que activa la función. No importa la hora que configuré, el usuario aún se desplazó a esa ubicación y se detuvo, activando el script repetidamente. Probé con un antirrebote básico y no funcionó; tuve problemas similares.
Aquí está mi guión:
let throttlePause; const throttle = (callback, time) => { if (throttlePause) return; throttlePause = true; setTimeout(() => { callback(); throttlePause = false; }, time); }; function scrollShrink() { document.getElementById('wrapper').classList.toggle('page-scrolled', window.pageYOffset >= 20); } // run through throttler window.addEventListener("scroll", () => { throttle(scrollShrink, 200); });