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

321
Visualizações
How to scroll to a span element inside a div in React, so it is always visible and not overflown

There is a div with a bunch of spans inside of it like so:

<div id="wordDisplayWrapper">
  {words.map((word, i) => {
    if (i === idx) {
      return (
        <span key={i} className={"currentWord"}>
          {word}
        </span>
      );
    } else {
      return (
        <span key={i} className={classes[i] || "word"}>
          {word}
        </span>
      );
    }
  })}
</div>;

The spans will overflow off the div, and overflow has been set to overflow-y: hidden;, this is the CSS:

#wordDisplayWrapper {
  height: 5.5em;
  overflow-y: hidden;
  outline: 1px solid orange;
}

The currentWord changes, and as it indexes forward, it overflows the div. I would like the div to scroll to this currentWord automatically, but I have not been able to find a way to do this. How should I go about this in React?

Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You may target the element to scroll using a a html selector / refs and call scrollIntoView(). Call scrollIntoView(true) if you want the current word to be at the top of the container.

More about Element.scrollIntoView: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView

If you want a transition for the scroll, add scroll-behavior: smooth; css rule to the container div.

About scroll-behavior: https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior

about 4 years ago · Juan Pablo Isaza Relatório
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