How to trigger the scroll using pure javascript?

2

I would like to know how to implement in pure javascript the same action used with jQuery below:

$('html, body').animate({
   scrollTop: 500
}, 300);

Can anyone tell me what the code looks like?

    
asked by anonymous 29.05.2018 / 16:22

1 answer

3

Using window.scroll , like this:

window.scroll({top: 500, left: 0, behavior: 'smooth' })

p {height: 40px; vertical-align: middel; border: solid 1px}
<h1>Rolar o html até o final...</h1>
<p>.....</p>
<p>.....</p>
<p>.....</p>
<p>.....</p>
<p>.....</p>
<p>.....</p>
<p>.....</p>
<p>.....</p>
<p>para encher a página...</p>
<p>.....</p>
<p>.....</p>
<p>.....</p>
<p>.....</p>
<p>.....</p>
<p>.....</p>
<p>.....</p>

<input type='button' onclick="window.scroll({top: 500, left: 0, behavior: 'smooth' })" value="clique aqui para executar o scroll" />

In addition, this feature is called "Smooth Scrolling" , and is documented in W3C: link

    
29.05.2018 / 16:28