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

119
Visualizações
Show/Hide each image of array of images on scroll height in React

I'm trying to show/hide individual images from an array of images based on the scroll height positioning in React. The idea being that at each interval the previous image will hide and the next one will show.

So far I've managed to map out the array of images and track the scroll position in my function component using the useEffect hook, I've also given props to each image so they have a min and max height value, then I've written an if statement that will check whether the image falls within the criteria. My issue seems to be passing the values of the min/max height to the if statement then displaying the correct image, or maybe something altogether that I've missed.

const ScrollingHeader = (props) => {

    const [imageVisible, setImageVisible] = useState(false);

    useEffect(() => {   
      window.addEventListener("scroll", showImage);
      return () => 
         window.removeEventListener("scroll", showImage); 
    }, [])

    const showImage = (props) => {
        console.log(window.scrollY);

        const minHeight = props.minHeight
        const maxHeight = props.maxHeight

        if (window.scrollY >= minHeight && window.scrollY <= maxHeight) {
            setImageVisible(true)
        } else {
            setImageVisible(false)
        }
    }

    return (
        <TeamImagesWrapper>
            <TeamSlideContainer>
            {props.imagesArray.map((images, i) => (
                <TeamImageSlide id={i} minHeight={i * 50 - 25} maxHeight={i * 50 + 25}>
                    { imageVisible &&
                    <img src={images.image.sizes.src} index={i} />
                    }
                </TeamImageSlide>
            ))}
            </TeamSlideContainer>
            <IntroPara>{props.introText}</IntroPara>
        </TeamImagesWrapper>
    )
}

export default ScrollingHeader
about 4 years ago · Juan Pablo Isaza
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