You can use .scroll()
to easily scroll elements or window. It has a built-in smooth scroll effect so basically the code couldn't be simpler.
Standard properties:
var options = {
top: 0, // Number of pixels along the Y axis to scroll the window or element
left: 0, // Number of pixels along the X axis to scroll the window or element.
behavior: 'smooth' // ('smooth'|'auto') - animate smoothly, or move in a single jump
}
DOCS: https://developer.mozilla.org/en-US/docs/Web/API/Window/scroll
SEE ALSO: .scrollIntoView()
https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
document.getElementById('btn').addEventListener('click',function(){
document.getElementById('container').scroll({top:0,behavior:'smooth'});
});
/*DEMO*/
#container{
width:300px;
max-height:300px;
padding:1rem;
margin-left:auto;
margin-right:auto;
background-color:#222;
color:#ccc;
text-align:justify;
overflow-y:auto;
}
#btn{
width:100%;
margin-top:1rem;
}
<div id="container">
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
<button id="btn">Scroll to top</button>
</div>
This is the only way it worked for me, with smooth scrolling transition:
$('html, body').animate({
scrollTop: $('#containerDiv').offset().top,
}, 250);
This worked for me :
document.getElementById('yourDivID').scrollIntoView();
When getting element by class name, don't forget the return value is an array; Hence this code:
document.getElementByClassName("dropdown-menu").scrollTop = 0
Would not work. Use code below instead.
document.getElementByClassName("dropdown-menu")[0].scrollTop = 0
I figured other people might encounter a similar problem as I did; so this should do the trick.
I tried the existing answers to this question, and none of them worked on Chrome for me. What did work was slightly different:
$('body, html, #containerDiv').scrollTop(0);
var myDiv = document.getElementById('containerDiv');
myDiv.innerHTML = variableLongText;
myDiv.scrollTop = 0;
See the scrollTop attribute.