最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Autoscroll div with jQuery to the bottom after appending - Stack Overflow

programmeradmin4浏览0评论

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!

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!

Share Improve this question edited Dec 20, 2012 at 4:13 Rab 35.6k4 gold badges51 silver badges66 bronze badges asked Jun 22, 2012 at 9:36 f1nnf1nn 7,04724 gold badges71 silver badges94 bronze badges 2
  • Could you provide the HTML or better yet create a Test case on jsFiddle? – AbstractChaos Commented Jun 22, 2012 at 9:45
  • #messages and .messages- did you mix them up? – Andreas Commented Jun 22, 2012 at 10:52
Add a comment  | 

3 Answers 3

Reset to default 8

Change

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

to

scrollTop: $("#messages").scrollHeight

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

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

Please try:

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

评论列表(0)

  1. 暂无评论