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

113
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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