Consider I have a list of questions. When I click on the first question, it should automatically take me to the bottom of the page.
For a matter of fact, I do know
You can use this function wherever you need to call it:
function scroll_to(div){
if (div.scrollTop < div.scrollHeight - div.clientHeight)
div.scrollTop += 10; // move down
}
jquery.com: ScrollTo
So many answers trying to calculate the height of the document. But it wasn't calculating correctly for me. However, both of these worked:
jquery
$('html,body').animate({scrollTop: 9999});
or just js
window.scrollTo(0,9999);
You can use this to go down the page in an animation format.
$('html,body').animate({scrollTop: document.body.scrollHeight},"fast");
You may try Gentle Anchors a nice javascript plugin.
Example:
function SomeFunction() {
// your code
// Pass an id attribute to scroll to. The # is required
Gentle_Anchors.Setup('#destination');
// maybe some more code
}
Compatibility Tested on:
If you want to scroll entire page to the bottom:
var scrollingElement = (document.scrollingElement || document.body);
scrollingElement.scrollTop = scrollingElement.scrollHeight;
See the sample on JSFiddle
If you want to scroll an element to the bottom:
function gotoBottom(id){
var element = document.getElementById(id);
element.scrollTop = element.scrollHeight - element.clientHeight;
}
And that's how it works:
Ref: scrollTop, scrollHeight, clientHeight
UPDATE: Latest versions of Chrome (61+) and Firefox does not support scrolling of body, see: https://dev.opera.com/articles/fixing-the-scrolltop-bug/
If any one searching for Angular
you just need to scroll down add this to your div
#scrollMe [scrollTop]="scrollMe.scrollHeight"
<div class="my-list" #scrollMe [scrollTop]="scrollMe.scrollHeight">
</div>