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

139
Visualizações
Fade in fade out images with scroll then scroll page content

When my page loads there is an image that will appear. What I want to do is on scroll, fade out that image and fade in another image. While this animation is happening, I don't want the images to be scrolled up. It's only when the second image has faded in completely that I want to be able to scroll to the content that follows on the page.

I used this answer to come up with part of a solution.

html

<body>
    <div id="container">
        <div id="mainImg">
            <img src="images/1.png" style="height: 100%">
        </div>
        <div id="brandStatement">
            <img src="images/2.png" style="height: 100%">
        </div>      
    </div>

    <img src="images/map.png">
    
    <script type="text/javascript" src="main.js"></script>
</body>

js

let locked         = false,
    mainImage      = document.getElementById('mainImg'),
    brandStatement = document.getElementById('brandStatement');

window.addEventListener('scroll', function() {
    if (!locked) {
        window.requestAnimationFrame(function() {

            brandStatement.style.opacity = Math.min(window.scrollY / window.innerHeight, 1);
            
            if (brandStatement.style.opacity === '1') {
                    // scroll to next content
            }

            locked = false;
        });
    }
    locked = true;
});

css

#container {
    height: 200vh;
    width: 100%;
}

#mainImg {
    text-align: center;
    width: 100%;
    height: 100%;
    position: fixed;
}

#brandStatement {
    text-align: center;
    width: 100%;
    height: 100%;
    position: fixed;
    opacity: 0;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I didn't see a possible solution to the problem by improving your code. This is a personal approach.

What I'm doing here, is changing the opacity of the element one inside the cover container as the user scrolls down the page, revealing the image below. After the opacity changes have been done, the script will change the filling container display style property from none to block. This element is just meant to fill the upper side of the cover container to prevent it from moving up when the position style property is changed from fixed to null.

And the reversed logic applies when scrolling back up.

const one = document.getElementById('one')
const cover = document.getElementById('cover')
const filling = document.getElementById('filling')

window.addEventListener('scroll', () => {
  let scrollY = window.scrollY
  let bottomHeight = window.innerHeight
  
  if(scrollY / bottomHeight <= 1){
    one.style.opacity = 1 - ( scrollY / bottomHeight )
    cover.style.position = 'fixed'
    filling.style.display = 'none'
  }
  else{
    cover.style.position = null
    filling.style.display = 'block'
  }
})
*{padding:0;margin:0;border-size: border-box}

body{
  height: 3500px;
}
img {
  width: 100%;
  height: 100vh;
}
#filling{
  height:100vh;
  width:100%
}
#cover{
  height:100vh;
  width:100%;
}
#cover > div{
  height:100vh;
  width:100%;
  position:absolute;
}
#one{
  z-index:2;
}
#two{
  z-index:1;
}
<body>

  <div id='filling' style='display:none'>
  </div>

  <div id='cover' style='position:fixed'>
    <div id='one'>
      <img src='https://picsum.photos/id/200/1000/1000'>
    </div>

    <div id='two'>
      <img src='https://picsum.photos/id/201/1000/1000'>
    </div>
  </div>  

  <div>
    <img src='https://picsum.photos/id/206/1000/1000'>
  </div>

  <div style='margin-top:-10px'>
    <img src='https://picsum.photos/id/204/1000/1000'>
  </div>

  <div style='margin-top:-10px'>
    <img src='https://picsum.photos/id/208/1000/1000'>
  </div>

</body>

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