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

115
Vistas
Smoothly scroll element from current position

I have written the following code to scroll an element 20 more pixels to the right.

const button = document.getElementById('slide');

button.onclick = function () {
  document.getElementById('container').scrollLeft += 20;
};

How can I make the scrolling smooth? I have tried using Element#scroll like so:

const button = document.getElementById('slide');

button.onclick = function () {
  document.getElementById('container').scroll({
      left: += 20,
      behavior: smooth
  });
};

Am I able to do this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use Element#scrollBy to scroll a certain amount from the current position.

button.onclick = function () {
  document.getElementById('container').scrollBy({
      left: 20,
      behavior: 'smooth'
  });
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

Is this what you are looking for?

From MDN:

scrollBy() scrolls by a particular amount, whereas scroll() scrolls to an absolute position in the document.

https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollBy

const container = document.querySelector("#container");
const button = document.querySelector("#btnScroll");

button.addEventListener('click', e => {
  container.scrollBy({
    left: 200,
    behavior: 'smooth'
  });
});
#container {
  position: relative;
  max-width: 200px;
  overflow-x: scroll;
  display: flex;
  margin: 20px;
}

img {
  display: inline-block
}
<div id="container">
  <img src="https://dummyimage.com/200x100/000/fff">
  <img src="https://dummyimage.com/200x100/0f0/000">
  <img src="https://dummyimage.com/200x100/00f/fff">
  <img src="https://dummyimage.com/200x100/f00/fff">
</div>
<button id="btnScroll">Scroll 200px</button>

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