2013-08-02 104 views
0

我有一些jQuery代碼每秒(因此,如果任何新的信息到達它們變得可見)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

的版本
+3

嘗試使用.prop(「scrollHeight」),而不是attr – Mihail

+2

也if條件沒有意義..'if(x + 20> x - 20)'??????? – Prasanth

+0

這個條件意味着它只會滾動,如果它可以滾動 – user2166538

回答

1

有沒有這樣的屬性scrollHeight(它的屬性)。如果你嘗試這樣的東西,而不是:

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

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

另一個祕訣:請確保您緩存您的DOM查詢像$box = $("#chatbox"),不要一次又一次地重新選擇要素。

+0

謝謝你,這工作正常我的一個曾經工作,所以我不知道發生了什麼大聲笑 – user2166538