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