简体   繁体   English

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

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

I have a div class='messages'. 我有一个div class ='messages'。 I add date to this div via jQuery.append() Here are the styles: 我通过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;
}

For autoscroll I use such function: 对于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);
}

About ~20 messages are scrolling normally, but after it 'hangs', new messages are not scrolled. 大约有20条消息正常滚动,但在“挂起”后,新消息不会滚动。 Chrome Version 19.0.1084.56 . Chrome版本19.0.1084.56。 What I'm doing wrong? 我做错了什么? Thanks! 谢谢!

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

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

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