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

232
Visualizações
Can't get coordinates of html element

I tried to get the coordinates of Block-1 and scroll the page to it, but I did something wrong =( How to get the html coordinates of the Block-1 element and scroll the page to it?

let elem = document.querySelector('.Block-1');
let coords = elem.getBoundingClientRect();
elem.style.top = coords.bottom + 'px';


window.scrollTo({
  top: elem,
  behavior: "smooth"
});
.Block-1 {
  margin-top: 500px;
  border: 1px solid black;
  font-size: 30px;
  padding: 50px;
}
<div class="Block-1">

  Scroll here

</div>

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

You where on the right track. The top property of the scrollTo() is the top property of the getBoundingClientRect(). So, top: coords.top.

let elem = document.querySelector('.Block-1:nth-child(2)');
let coords = elem.getBoundingClientRect();

window.scrollTo({
  top: coords.top,
  behavior: "smooth"
});
.Block-1 {
  margin-top: 500px;
  border: 1px solid black;
  font-size: 30px;
  padding: 50px;
}
<div class="Block-1">

  Not here

</div>

<div class="Block-1">

  Scroll here

</div>

<div class="Block-1">

  Not here

</div>

about 4 years ago · Santiago Gelvez 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