f1nn
f1nn

Reputation: 7047

Autoscroll div with jQuery to the bottom after appending

I have a div class='messages'. I add date to this div via jQuery.append() Here are the styles:

.messages {
border: 1px solid #dddddd;
padding:10px;
height: 400px;
overflow-x:visible;
overflow-y: scroll;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
margin-bottom:10px;
font-size:14px;
}

For autoscroll I use such function:

receiveMessage = function (name, message, type) {
    //adding new message
    $("#messages").append('<strong>' + name + ": " + '</strong>' + message + '<br>');
    /autoscrolling to the bottom
    $("#messages").animate({
        scrollTop: $("#messages").height()
    }, 300);
}

About ~20 messages are scrolling normally, but after it 'hangs', new messages are not scrolled. Chrome Version 19.0.1084.56 . What I'm doing wrong? Thanks!

Upvotes: 13

Views: 27207

Answers (3)

Chintan Patel
Chintan Patel

Reputation: 21

Please try:

$(document).ready(function(){
   $('#chat-scroll').animate({scrollTop: $('#chat-scroll')[0].scrollHeight}, 2000);
});

Upvotes: 2

Jeffrey L. Roberts
Jeffrey L. Roberts

Reputation: 2994

This solution did not work for me, however, the following did...

$(document).ready(function(){
   $('#chat-scroll').animate({
   scrollTop: $('#chat-scroll').get(0).scrollHeight}, 2000); 
 });

Upvotes: 3

Rab
Rab

Reputation: 35572

Change

scrollTop: $("#messages").height()

to

scrollTop: $("#messages").scrollHeight

Upvotes: 8

Related Questions