Scroll to bottom of div?
Solution 1
Here's what I use on my site:
var objDiv = document.getElementById("your_div");
objDiv.scrollTop = objDiv.scrollHeight;
Solution 2
This is much easier if you're using jQuery scrollTop:
$("#mydiv").scrollTop($("#mydiv")[0].scrollHeight);
Solution 3
Try the code below:
const scrollToBottom = (id) => {
const element = document.getElementById(id);
element.scrollTop = element.scrollHeight;
}
You can also use Jquery to make the scroll smooth:
const scrollSmoothlyToBottom = (id) => {
const element = $(`#${id}`);
element.animate({
scrollTop: element.prop("scrollHeight")
}, 500);
}
Here is the demo
Here's how it works:
Ref: scrollTop, scrollHeight, clientHeight
Solution 4
using jQuery animate:
$('#DebugContainer').stop().animate({
scrollTop: $('#DebugContainer')[0].scrollHeight
}, 800);
Solution 5
Newer method that works on all current browsers:
this.scrollIntoView(false);
kush
My work calls me a Senior Software Engineer I know Ruby, JS (vue.js, etc), Erlang/Elixir, HTML/CSS. I have built side projects in Rust, Clojure (my love), and Go. I've been using Linux and MacOS forever and know both really well (although some linux are weird). I'm technically a front-end dev but I've run my own web businesses where I did everything from design, marketing, PR, server, ets.
Updated on July 30, 2021Comments
-
kush almost 3 years
I am creating a chat using Ajax requests and I'm trying to get messages div to scroll to the bottom without much luck.
I am wrapping everything in this div:
#scroll { height:400px; overflow:scroll; }
Is there a way to keep it scrolled to the bottom by default using JS?
Is there a way to keep it scrolled to the bottom after an ajax request?
-
jondinham over 12 yearsis this method ok with all browsers?
-
Jimmy Bosse almost 12 yearsyou need [0] to get dom element from jquery element to get scrollHeight
-
Sean almost 12 years@PaulDinh: I just looked into this and there is an issue with IE7 and lower using scrollHeight. There does seem to be a work around for IE7 here.
-
kalyfe almost 11 yearsNotice how this answer uses .stop(), which prevents issues with multiple animations.
-
Eric over 10 yearsWithout repeating yourself:
$("#mydiv").scrollTop(function() { return this.scrollHeight; });
-
Irfan Yusanif over 8 yearshow to get dynamic id? like in
<div class="abc"><div data-bind=attr : {'id': myId } ></div></div>
In this code myId is a variable. How can I access this id in script. -
Benkinass over 8 yearsI'm not quite sure I understand your question. In my example, "myId" is the id of the scroll container. Do you want to create more than one area where the user can scroll ?
-
SStanley almost 8 yearsI suspect $scope.$apply(callback) would work as well as this forces a digest and re-evaluation of the view.
-
Wayferer almost 6 yearsThank you! I was really wondering why I couldn't get it to work and the $timeout was the issue.
-
aiven almost 6 yearswhy not
scrollTopMax
instead ofscrollHeight
? -
john ktejik about 5 yearsThis isn't working when I dynamically add images :(
-
Mahdi Bagheri almost 5 yearschange scrollTopMax to var maxScrollTop = element[0].scrollHeight - element.outerHeight();
-
KingRider over 4 years@Wayferer same
setTimeout(function() { ... }, n)
-
MichielB almost 4 yearsThe 'modern' way is to use Element.scrollIntoView() -- developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
-
rbansal almost 4 yearsThis should be the accepted answer as the older methods do not work anymore
-
mplungjan almost 4 yearsThat would be the case if "this" is something in the div that can be scrolled to
-
defend orca almost 4 yearsthis is really right answer, the pic is really useful
-
Lucas Breitembach almost 4 yearscan you use var element = document.getElementById("elementToSelect"); element.scrollIntoView();
-
Anjali Sharma over 3 yearswith setTimeout(), its working fine. Worked with angular 9. Saved my day.
-
ngShravil.py over 3 yearsHappy to help :)
-
Evgeniy Boytsov over 3 yearslooks like a hack. what does the 1000000 number mean?
-
Richard Tyler Miles over 3 yearsThis didn't work for me, If you have query use @andsien.. Note an alternative animate can have unedited behavior with the scroll bar locking in safari <= 14.0.1 (latest)
-
Gil Epshtain over 3 yearsThe method
scrollIntoView
is triggered on a child element inside a parent container. So how to use it to: scroll parent to bottom (as asked in the original question). -
giovannipds over 3 yearsYou can use
scrollIntoView({ behavior: "smooth", block: "end" });
-
Akira Yamamoto over 3 yearsSee which browsers are supported here caniuse.com/?search=scrollIntoView
-
Kayote about 3 yearsGreat answer, tested and working well. Thank you.
-
Cybernetic about 2 yearsThis scrolls the whole page, not the targeted element (chrome)