Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
Javascript for fading an image on-scroll is still showing the image on page load

I've got an image in a fixed-position div. The following code is fading the image in where I want to, but initially the image is visible. Once I start scrolling, it disappears and then reappears when it should. Trying to figure out why the image is displaying on page load.

    const checkpoint = 500;

    window.addEventListener("scroll", () => {
      const currentScroll = window.pageYOffset;
      if (currentScroll <= checkpoint) {
        opacity = 0 - currentScroll / checkpoint;
      } else {
        opacity = 1;
      }
      document.querySelector(".sticky-logo-fade-in").style.opacity = opacity;
    });
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can store the callback in a variable, then call it on page load:

const checkpoint = 500;

const scrollHandler = () => {
    const currentScroll = window.pageYOffset;
    if (currentScroll <= checkpoint) {
        opacity = 0 - currentScroll / checkpoint;
    } else {
        opacity = 1;
    }
    document.querySelector(".sticky-logo-fade-in").style.opacity = opacity;
}

window.addEventListener("scroll", scrollHandler);

scrollHandler();
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda