Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
cómo ajustar el ancho de un div para expandirlo y reducirlo al desplazarse

Estaba haciendo esto y pensé que era fácil hasta que me quedé atascado.
Quiero expandir un div con desplazamiento hacia abajo hasta el 100% hasta la parte inferior del div, luego en la parte inferior se reduce al 90% y en la parte superior de desplazamiento hacia atrás
He intentado usar: biblioteca de paralaje que no fue útil
aquí hay una muestra en vivo: https://stackblitz.com/edit/react-ybdmbn?file=src/components/AdjustContainer.jsx

he intentado usar el gancho useEffect, como a continuación

 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 answers
Answer question

0

Parece que en lugar de simplemente verificar si el usuario se ha desplazado, debe verificar si el div está a la vista y cambiar el ancho en consecuencia.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!