jquery动画没有动画

时间:2013-08-02 19:58:12

标签: javascript jquery animation jquery-animate

我有一些jquery代码会每秒加载我网站的聊天框(所以如果有任何新帖子到达它们会变得可见)

我的代码在这里

function loadLog(){     
    $.ajax({
        url: "/log.html",
        cache: false,
        success: function(html){        
            $("#chatbox").html(html); //Insert chat log into the #chatbox div           
            if($("#chatbox").attr("scrollHeight") + 20 > $("#chatbox").attr("scrollHeight") - 20){
                $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
            }               
        },
    });
}

一切正常,除了它是自动滚动到聊天框的底部,所以你看到最新的帖子,而不是它只是停留在顶部。

我使用的是最新版本的jQuery

1 个答案:

答案 0 :(得分:1)

没有这样的属性scrollHeight(它的属性)。如果你尝试这样的事情会怎么样:

$box.animate({scrollTop: $box[0].scrollHeight}, 'normal');

http://jsfiddle.net/dfsq/zBdas/

另一个提示:确保缓存您的DOM查询,例如$box = $("#chatbox"),不要一次又一次地重新选择元素。

相关问题