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

141
Views
Change URL when scroll into sections

i'm using this code to highlight the navbar when scrolling into sections. Is it possible to change the URL in the address bar instantly when I scroll to an section? Can anyone help me?

Thanks in advance

const [current, setCurrent] = useState('')

const sections = document.querySelectorAll('h3')
const navLi = document.querySelectorAll('nav ul li')

  window.addEventListener('scroll', () => {
    sections.forEach(section => {
      const sectionTop = section.offsetTop 
      if (window.pageYOffset >= sectionTop && (window.pageYOffset + 100) - sectionTop < 200 && current !== section.getAttribute('id')) {
 setCurrent(section.getAttribute('id'))
        
      }
    })
  })

  useEffect(() => {
    navLi.forEach(li => {
      li.classList.remove('test')
      if(li.classList.contains(current)) {
        li.classList.add('test')
      }
    })
  }, [current])

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

0

Use an intersection observer :) Try this locally with a small borwser window. It will not work with StackOverflow snippets due to CORS / iframe origin issues, the snippets are run in an iframe.

<p class="x">Bloop1</p>
<p>Bleep</p><p>Bleep</p><p>Bleep</p><p>Bleep</p>
<p>Bleep</p><p>Bleep</p><p>Bleep</p><p>Bleep</p>
<p>Bleep</p><p>Bleep</p><p>Bleep</p><p>Bleep</p>
<p>Bleep</p><p>Bleep</p><p>Bleep</p><p>Bleep</p>
<p class="x">Bloop2</p>
<p>Bleep</p><p>Bleep</p><p>Bleep</p><p>Bleep</p>
<p>Bleep</p><p>Bleep</p><p>Bleep</p><p>Bleep</p>
<p>Bleep</p><p>Bleep</p><p>Bleep</p><p>Bleep</p>
<p>Bleep</p><p>Bleep</p><p>Bleep</p><p>Bleep</p>
<p class="x">Bloop3</p>
<p>Bleep</p><p>Bleep</p><p>Bleep</p><p>Bleep</p>
<p>Bleep</p><p>Bleep</p><p>Bleep</p><p>Bleep</p>
<p>Bleep</p><p>Bleep</p><p>Bleep</p><p>Bleep</p>
<p>Bleep</p><p>Bleep</p><p>Bleep</p><p>Bleep</p>
<script>
  // which elements to keep track of when they scroll into view
  const elements = document.querySelectorAll(".x");

  // this is observing when the element will come into view
  const observer = new IntersectionObserver((x) => {
    // this is just to get an example
    const text = x[0].target.textContent;
    console.log(text);

    // this actually changes the URL
    window.history.pushState(text, "Title", "/" + text);
  });

  // register the observer for each element we are interested in
  elements.forEach((x) => {
    observer.observe(x);
  });
</script>
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!