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

236
Visualizações
NextJS: hash id anchor does not working in some moments

I was trying to move user to a certain position (which a HTMLElement with a given id is) when user click a button. So far I have tried using hashtag directly and using hash tag and add a listener listen to the router change in useEffect, and send user to the element if hashtag is detected and that element is not in the view port. They are not working at most of the time. I added a while loop to keep triggering the function, which results bringing user to the element after 20+ calls in 7-8 seconds. That is just annoying.

Does anyone successfully make the hashtag works? How can that be done?

What I have tried so far: hashtag link: failed hashtag link with a slash before the hashtag: failed, can't see a difference function moving user to the location: failed, but the function itself is successfully triggered

What I am observing at the moment:

  1. User is not moved at all
  2. User is moved, but the process suddenly stops before user actually reach that element. I used smooth behavior as my global css, so this effect is really strange to me.
  3. Keep checking if user is not reaching that position, and send them to it if checked. Not working as expected. It does check and send user and send again if another check still fails, but the process is too long (7-8 seconds, or longer) and if user scroll in the process they will still be dragged to the element if they haven't reach it.

Related code:


  function isInViewport(el: HTMLElement) {
    const rect = el.getBoundingClientRect();
    return (
        rect.top >= 0 &&
        rect.left >= 0 &&
        rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
        rect.right <= (window.innerWidth || document.documentElement.clientWidth)
      );
  }
  
  useEffect(() => {
    const path = router.asPath;
    if (path && path.includes('/#')) {
      const sectionName = path.replace('/#', '');
      const ele = document.getElementById(sectionName);
      if (ele) {
        const interval = setInterval(() => {
          if (!isInViewport(ele)) {
            console.log('triggered');
            setTimeout(() => {
              ele.scrollIntoView({ behavior: "smooth" })
            }, 2000)
          }
          else {
            clearInterval(interval);
          }
        }, 2000);
      }
    }
  }, [router.asPath]);
about 4 years ago · Juan Pablo Isaza
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