267

I have a div on my page:

<div id='div1' style='overflow:scroll;overflow-x:hidden;max-height:200px;'></div>

How can I make the div scroll to the bottom of the div?? Not the page, just the DIV.

sudo bangbang
  • 27,127
  • 11
  • 75
  • 77
DobotJr
  • 3,929
  • 9
  • 36
  • 49

15 Answers15

701

The other solutions here don't actually work for divs with lots of content -- it "maxes out" scrolling down to the height of the div (instead of the height of the content of the div). So they'll work, unless you have more than double the div's height in content inside of it.

Here is the correct version:

$('#div1').scrollTop($('#div1')[0].scrollHeight);

or jQuery 1.6+ version:

var d = $('#div1');
d.scrollTop(d.prop("scrollHeight"));

Or animated:

$("#div1").animate({ scrollTop: $('#div1').prop("scrollHeight")}, 1000);
Logan Hasbrouck
  • 416
  • 1
  • 7
  • 21
Mike Todd
  • 7,227
  • 2
  • 15
  • 11
73

All the answers that I can see here, including the currently "accepted" one, is actually wrong in that they set:

scrollTop = scrollHeight

Whereas the correct approach is to set:

scrollTop = scrollHeight - clientHeight

In other words:

$('#div1').scrollTop($('#div1')[0].scrollHeight - $('#div1')[0].clientHeight);

Or animated:

$("#div1").animate({
  scrollTop: $('#div1')[0].scrollHeight - $('#div1')[0].clientHeight
}, 1000);
patspam
  • 2,069
  • 18
  • 6
  • I have a div with a set height and overflow set to auto. The animated answers worked but this is the only non-animated solution that would actually scroll to the bottom of the "content", not the set height of the div. Bravo! – Darkloki Apr 24 '15 at 12:07
27

UPDATE : see Mike Todd's solution for a complete answer.


$("#div1").animate({ scrollTop: $('#div1').height()}, 1000);

if you want it to be animated (over 1000 milliseconds).

$('#div1').scrollTop($('#div1').height())

if you want it instantaneous.

Community
  • 1
  • 1
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
15
$(window).load(function() {
  $("html, body").animate({ scrollTop: $(document).height() }, 1000);
});

This grabs the height of the page and scrolls it down once the window has loaded. Change the 1000 to whatever you need to do it faster/slower once the page is ready.

Chords
  • 6,720
  • 2
  • 39
  • 61
12

None of these worked for me, I have a message system inside a web app that's similar to Facebook messenger and wanted the messages to appear at the bottom of a div.

This worked a treat, basic Javascript.

window.onload=function () {
     var objDiv = document.getElementById("MyDivElement");
     objDiv.scrollTop = objDiv.scrollHeight;
}
James Blachford
  • 121
  • 1
  • 4
7

try this:

$('#div1').scrollTop( $('#div1').height() )
Ram
  • 143,282
  • 16
  • 168
  • 197
6

The following will work. Please note [0] and scrollHeight

$("#myDiv").animate({ scrollTop: $("#myDiv")[0].scrollHeight }, 1000);
Alex Gittemeier
  • 5,224
  • 30
  • 55
Lal Kokkat
  • 61
  • 1
  • 1
5

Scroll window to the bottom of target div.

function scrollToBottom(id){
  div_height = $("#"+id).height();
  div_offset = $("#"+id).offset().top;
  window_height = $(window).height();
  $('html,body').animate({
    scrollTop: div_offset-window_height+div_height
  },'slow');
}

scrollToBottom('call_div_id');
Typist
  • 1,464
  • 9
  • 14
jenking
  • 51
  • 1
  • 2
5

for animate in jquery (version > 2.0)

var d = $('#div1');
d.animate({ scrollTop: d.prop('scrollHeight') }, 1000);
Luca
  • 9,259
  • 5
  • 46
  • 59
dpineda
  • 2,401
  • 27
  • 25
3

I'm working in a legacy codebase trying to migrate to Vue.

In my specific situation (scrollable div wrapped in a bootstrap modal), a v-if showed new content, which I wanted the page to scroll down to. In order to get this behaviour to work, I had to wait for vue to finish re-rendering, and then use jQuery to scroll to the bottom of the modal.

So...

this.$nextTick(function() {
    $('#thing')[0].scrollTop = $('#thing')[0].scrollHeight;
})
laaksom
  • 2,050
  • 2
  • 18
  • 17
0

You can check scrollHeight and clientHeight with scrollTop to scroll to bottom of div like code below.

$('#div').scroll(function (event) {
  if ((parseInt($('#div')[0].scrollHeight) - parseInt(this.clientHeight)) == parseInt($('#div').scrollTop())) 
  {
    console.log("this is scroll bottom of div");
  }
  
});
Nghi
  • 1
0
$(document).ready(function() {
    let width = $(window).width();
    let element = $("#YourId");
    let positionFromTop = element.offset().top + element.prop("scrollHeight");
    $("html, body").animate({
        scrollTop: Math.abs($(window).height() - positionFromTop)
    }, 500);
});
Chris Aelbrecht
  • 2,051
  • 21
  • 25
0

If you're looking to simply scroll to the bottom of a div, then just use an huge out of range integer, no css or height reference is needed.

$("#ScrollabledDiv").scrollTop(200000)

Dylan
  • 2,161
  • 2
  • 27
  • 51
Andyroo
  • 11
  • 1
-1

You can use below code to scroll to bottom of div on page load.

$(document).ready(function(){
  $('div').scrollTop($('div').scrollHeight);
});
Manish Nagdewani
  • 597
  • 1
  • 3
  • 14
-1

When page is load then scroll is max value .

This is message box when user send message then always show latest chat in down so that scroll value is always is maxium.

$('#message').scrollTop($('#message')[0].scrollHeight);

see image

TAbdiukov
  • 1,185
  • 3
  • 12
  • 25