Scroll to bottom of div?

991,916

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:

enter image description here

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);
Share:
991,916
kush
Author by

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, 2021

Comments

  • kush
    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
    jondinham over 12 years
    is this method ok with all browsers?
  • Jimmy Bosse
    Jimmy Bosse almost 12 years
    you need [0] to get dom element from jquery element to get scrollHeight
  • Sean
    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
    kalyfe almost 11 years
    Notice how this answer uses .stop(), which prevents issues with multiple animations.
  • Eric
    Eric over 10 years
    Without repeating yourself: $("#mydiv").scrollTop(function() { return this.scrollHeight; });
  • Irfan Yusanif
    Irfan Yusanif over 8 years
    how 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
    Benkinass over 8 years
    I'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
    SStanley almost 8 years
    I suspect $scope.$apply(callback) would work as well as this forces a digest and re-evaluation of the view.
  • Wayferer
    Wayferer almost 6 years
    Thank you! I was really wondering why I couldn't get it to work and the $timeout was the issue.
  • aiven
    aiven almost 6 years
    why not scrollTopMax instead of scrollHeight?
  • john ktejik
    john ktejik about 5 years
    This isn't working when I dynamically add images :(
  • Mahdi Bagheri
    Mahdi Bagheri almost 5 years
    change scrollTopMax to var maxScrollTop = element[0].scrollHeight - element.outerHeight();
  • KingRider
    KingRider over 4 years
    @Wayferer same setTimeout(function() { ... }, n)
  • MichielB
    MichielB almost 4 years
    The 'modern' way is to use Element.scrollIntoView() -- developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoV‌​iew
  • rbansal
    rbansal almost 4 years
    This should be the accepted answer as the older methods do not work anymore
  • mplungjan
    mplungjan almost 4 years
    That would be the case if "this" is something in the div that can be scrolled to
  • defend orca
    defend orca almost 4 years
    this is really right answer, the pic is really useful
  • Lucas Breitembach
    Lucas Breitembach almost 4 years
    can you use var element = document.getElementById("elementToSelect"); element.scrollIntoView();
  • Anjali Sharma
    Anjali Sharma over 3 years
    with setTimeout(), its working fine. Worked with angular 9. Saved my day.
  • ngShravil.py
    ngShravil.py over 3 years
    Happy to help :)
  • Evgeniy Boytsov
    Evgeniy Boytsov over 3 years
    looks like a hack. what does the 1000000 number mean?
  • Richard Tyler Miles
    Richard Tyler Miles over 3 years
    This 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
    Gil Epshtain over 3 years
    The 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
    giovannipds over 3 years
    You can use scrollIntoView({ behavior: "smooth", block: "end" });
  • Akira Yamamoto
    Akira Yamamoto over 3 years
    See which browsers are supported here caniuse.com/?search=scrollIntoView
  • Kayote
    Kayote about 3 years
    Great answer, tested and working well. Thank you.
  • Cybernetic
    Cybernetic about 2 years
    This scrolls the whole page, not the targeted element (chrome)