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

196
Views
¿Cómo obtener la ID de div a la que me desplazo?

Quiero obtener la identificación de cada div a la que me desplazo y mantener la identificación en mi estado.

Tengo una lista de algunos elementos div. Espero almacenar en mi estado la ID actual del div al que me desplazo.

Mi código

 import React, { useEffect, useState } from "react"; export default function App() { const [currentId, setCurrentId] = useState(null); useEffect(() => { // some code here }, []); return ( <> <div style={{ height: 600, background: "#d7a1a1" }} id="div-1"> <h1>div 1</h1> </div> <div style={{ height: 600, background: "#d7a1a1" }} id="div-2"> <h1>div 2</h1> </div> <div style={{ height: 600, background: "#d7a1a1" }} id="div-3"> <h1>div 3</h1> </div> <div style={{ height: 600, background: "#d7a1a1" }} id="div-4"> <h1>div 4</h1> </div> <div style={{ height: 600, background: "#d7a1a1" }} id="div-5"> <h1>div 5</h1> </div> </> ); }

Entonces, cuando me desplazo al primer div, quiero almacenar en currentId state div-1 , al segundo div-2 y así sucesivamente.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puedes hacer algo así:

 const [currentId, setCurrentId] = useState(null) const [scrollPos, setScrollPos] = useState(0) useEffect(() => { window.addEventListener('scroll', setScrollPos(window.pageYOffset)) if(scrollPos <= 600) setCurrentId('div-1') else if(scrollPos > 600 && scrollPos <= 1200) setCurrentId('div-2') else if(scrollPos > 1200 && scrollPos <= 1800) setCurrentId('div-3') // and so .... return () => { window.removeEventListener('scroll', handleScroll) } },[])
over 4 years ago · Santiago Trujillo 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!