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

154
Views
cómo resaltar la barra de navegación mientras se desplaza

Tengo este código HTML básico que tiene secciones, y luego creé una barra de navegación básica dinámicamente por javaScript

 function buildNav() { for (section of sections) { let item = document.createElement("li"); item.innerHTML = `<a data-nav="${section.id}">${section.dataset.nav}</a>`; item.classList.add("item"); } navBar.appendChild(item); } }

y usé esta función para agregar una clase activa a cada sección

 function isInView(element) { const rect = element.getBoundingClientRect(); return rect.top >= -70 && rect.top <= 300; }; function addActive() { document.addEventListener( "scroll", () => { for (section of sections) { if(isInView(section)){ section.classList.add('active') }else{section.classList.remove('active')} }; }) }

mi pregunta es ¿cómo puedo resaltar el elemento en la barra de navegación que representa la sección a la vista?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Debe aplicar la clase activa solo al elemento que coincida con la sección actual de la página. Prueba esto: -

 function isInView(element) { const rect = element.getBoundingClientRect(); if(rect.top > 70 && rect.top < 150){ //Assumed Y-offset for 1st section return element.id //Assuming it returns 1 for 1st element } else if(rect.top > 150){ return element.id //Returns 2 for 2nd element } }; function addActive() { document.addEventListener( "scroll", () => { for (section of sections) { if(isInView(section) == section.id){ section.classList.add('active') //Only apply active class to this section } else{ section.classList.remove('active') //Remove the active class from all other sections } }; }) }
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!