How to make auto scroll to top of the div?

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 ?

Answers 1

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


Related Articles