Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

243
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda