简体   繁体   中英

How to make auto scroll to top of the div?

this is my screen shot :

在此处输入图片说明

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

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> 

请尝试以下方法:

window.scrollTo(0,0);

Give an id to your div. And then:

var myDiv = document.getElementById('yourdivid');
myDiv.scrollTop = 0;

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM