简体   繁体   English

jQuery动画不动画

[英]jquery animation does not animate

i have some jquery code that will load the chatbox of my site every second (so if any new posts arrive they become visible) 我有一些jquery代码将每秒加载我网站的聊天框(因此,如果有任何新帖子到来,它们就会变得可见)

my code is here 我的代码在这里

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

everything works fine, except it is meant to autoscroll to the bottom of the chatbox so you see the newest posts, instead it just stays at the top. 一切工作正常,除了它打算自动滚动到聊天框的底部,以便您看到最新的帖子,而只是停留在顶部。

I am using the most recent version of jQuery 我正在使用最新版本的jQuery

There is no such attribute scrollHeight (it's property). 没有此类属性scrollHeight (它的属性)。 What if you try something like this instead: 如果您尝试这样的操作,该怎么办:

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

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

Another tip: make sure you cache your DOM queries like $box = $("#chatbox") , don't reselect elements again and again. 另一个提示:确保缓存$box = $("#chatbox")类的DOM查询,不要一次又一次地重新选择元素。

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

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