繁体   English   中英

添加后,使用jQuery自动滚动div到底部

[英]Autoscroll div with jQuery to the bottom after appending

我有一个div class ='messages'。 我通过jQuery.append()向这个div添加日期以下是样式:

.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;
}

对于autoscroll我使用这样的功能:

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);
}

大约有20条消息正常滚动,但在“挂起”后,新消息不会滚动。 Chrome版本19.0.1084.56。 我做错了什么? 谢谢!

更改

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

scrollTop: $("#messages").scrollHeight

这个解决方案对我不起作用,但是,以下做了......

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

请试试:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM