Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda