3

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
  • 387
  • 2
  • 7
  • 21

4 Answers4

11

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
  • 72,925
  • 12
  • 73
  • 99
2

Please try this:

window.scrollTo(0,0);
Nayana_Das
  • 1,740
  • 4
  • 23
  • 43
2

Give an id to your div. And then:

var myDiv = document.getElementById('yourdivid');
myDiv.scrollTop = 0;
Emre
  • 297
  • 1
  • 3
  • 16
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