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

195
Visualizações
how to adjust a div width to expand and shrink back on scroll

I was doing this and thought its easy until I got stuck.
I want to expand a div with on scroll down to 100% until the bottom of the div then at the bottom shrink back to 90% and on scroll top doing reverse
I have tried using: parallax library which wasn't helpful
here is a live sample: https://stackblitz.com/edit/react-ybdmbn?file=src/components/AdjustContainer.jsx

have tried to use the useEffect hook, like below

const [adjustWidth, setAdjustWidth] = useState()

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


    const handleScroll = (e) => {
        let scrollTop = window.scrollY
        if (scrollTop !== 0) {
            // console.log('e', e);
            setAdjustWidth(
                {
                    width: '100%',
                    transition: 'width 2s'
                }
            )
        } else {
            setAdjustWidth(
                {
                    width: '90%',
                    transition: 'width 2s'
                }
                )
        }
    }

      <div className={styles.AdjustContainer}  style={adjustWidth}>

           my content
      </div>


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

0

Looks like instead of just checking if the user has scrolled, you need to check whether the div is in view and change the width accordingly.

import React, { useEffect, useState, useRef } from 'react';
    
const AdjustContainer = ({ children }) => {
  const ele = useRef();
  const [width, setWidth] = useState('90%');

  useEffect(() => {
    const observer = new IntersectionObserver(handleIntersection);

    observer.observe(ele.current);

    return () => {
      observer.unobserve(ele.current);
    };
  }, []);

  const handleIntersection = (entries) => {
    entries.map((entry) => {
      entry.isIntersecting ? setWidth('100%') : setWidth('90%');
    });
  };

  return (
    <div
      ref={ele}
      className="adjustContainer"
      style={{ width, transition: 'width 2s' }}
    >
      {children}
    </div>
  );
};
export default AdjustContainer;
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