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

134
Visualizações
Widen Image on scroll down using React

I want an image animation effect while scrolling using react.js, something similar to this : https://www.roche.com/ I have this code :

<ArticleWrapper >
        <img  className='image' src={Img} alt='Image' />
</ArticleWrapper >

I want the img to be widen animated while scrolling down and returns to normal when scrolling up ! Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

One of the possible solutions with runnable example

This is one of the way to do that:

  • Wrap your img with a padded and flex-centered container
  • Give width: 100% to your img, to fit the container
  • Give a padding to your container (to get your image small as you want it at start)
  • Setup a scroll listener
  • Compute a new padding value for your image container
  • Update the container with the new padding value

In the snippet i have 2 important variables:

  • maxPadding the padding value (higher = smaller img)
  • maxScrollFor100 the point (in px) where the scroll is complete aka big img

Here we have the runnable snippet

const maxScrollFor100 = 300; // Y Scroll Point where the image should be 100% width
const maxPadding = 100; // Initial padding of the container 
const imgContainer = document.getElementById('img-container');
imgContainer.style.padding = '0 ' + maxPadding + 'px';

window.addEventListener('scroll', function(event) {
  // Get the current scrollY point
  const sY = window.scrollY; 
  
  // Get a padding percentage (we want 100% with 0 scroll and 0% with 300 or + scroll)
  const percent = 100 - (sY >= maxScrollFor100 ? 100 : sY / (maxScrollFor100 / 100));
  
  // console.log("Actual Percent Zoom", percent.toFixed(1)); // Show the current zoom percentage.

  // Compute the new padding value
  const padding = maxPadding * (percent / 100);

  // Update the dom with the new padding for the image container
  imgContainer.style.padding = '0 ' + padding + 'px';
}, {passive: true})
.flex-center {
  display: flex;
  justify-content: center;
  align-items: center;
}
<div style="padding-top: 150px">
  <div id="img-container" class="flex-center">
    <img src="http://www.pixolo.it/wp-content/uploads/2012/11/stones-and-sea-1920x1200-wallpaper-6620.jpg" width="100%" />
  </div>
</div>

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