4

this is my screen shot :

enter image description here

My question is how to make it auto scrolling back to top in javascript or any other way ?

Reynald Henryleo
  • 397
  • 2
  • 7
  • 22

4 Answers4

12

element.scrollTop = 0; will let you get to the element's scroll top:

var el = document.querySelector('div');
el.scrollTop = el.scrollHeight;

setTimeout(function(){
  el.scrollTop = 0;
}, 500);
div{width:200px; height:200px; overflow:auto;}
<div>
<br>Top-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-<br>-
</div>
Jai
  • 74,255
  • 12
  • 74
  • 103
4

Please try this:

window.scrollTo(0,0);
Nayana_Das
  • 1,789
  • 4
  • 23
  • 48
2

Give an id to your div. And then:

var myDiv = document.getElementById('yourdivid');
myDiv.scrollTop = 0;
Emre
  • 337
  • 1
  • 3
  • 18
1

If you need to animate, then try the below

$('html, body').animate({scrollTop: '0px'}, 2000);

If its a div you want to animate,

$('#yourid').animate({scrollTop: '0px'}, 1000);
CaptainHere
  • 698
  • 6
  • 14