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

135
Visualizações
Why can't I scroll to the bottom of my div?

so basicaly, i'm doing a chat in vuejs + socket.io and I want that every time a message is sent, received, or when you come to the site, you arrive directly to the bottom of the page, where the latest message is

I've done a function that fires on the onMounted hook, when I retrieve the data, and on the socket.on('message') :

        const scrollToLatest = () => {
            let container = document.getElementById("messages")
            console.log("scrolltop",container.scrollTop)
            console.log("scrollheight",container.scrollHeight)
            container.scrollTop = container.scrollHeight;
            console.log("scrolltop after", container.scrollTop)
        } 

Here are what the console.log returns :

(if you can't load the image :)

scrolltop 0
scrollheight 5594
scrolltop after 0

I've tried to use window.scrollTo(0, container.scrollHeight) but it doesn't work either

I hope that someone will be able to help me, I've searched a lot on this issue, but I wasn't able to make it work properly

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

0

pass a custom class to your div in your <template> first:

<div :class="`messages`">
</div>

and then get the position of the div as a number:

getPositionOfElement() {
  return (document.getElementsByClassName("messages").getBoundingClientRect().top - window.innerHeight);
}

This would be the method for scrolling to the position where your div is located - adapt it to your needs as described in the official documentation:

smoothScroll(vertPos) {
  window.scrollTo({
    top: vertPos,
    left: 0,
    behavior: 'smooth'
  });
}

combine both methods like this from the place where you need to perform the scrolling - calling this either from another method or directly from the template...

this.smoothScroll(this.getPositionOfElement());

Please let me know if this works for you.

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