2012-08-23 90 views
-2

我有一個div,它是一個聊天窗口。隨着人們發送消息並將它們追加到內容的末尾,我希望內容滾動到底部(以便最新的聊天消息可見)。滾動到div中內容的底部

<div id="chat"> 

</div><!--chat end--> 
+0

你的意思是什麼底部?滾動條應該在上下的div的一邊... – Lix

+0

我的意思是,讓它滾動下來 – southpaw93

+0

關於第二個想法...也許你應該看看這個答案 - http://stackoverflow.com/ a/270628/558021 – Lix

回答

0

您可以將CSS值scrollTop設置爲容器的內容高度(或scrollHeight)。

下面是示例代碼和我用它在此琴:http://jsfiddle.net/aHx2Z/

// get the new height of the chat window 
var height = $('#chat_window')[0].scrollHeight; 
$('#chat_window').animate({scrollTop:height}, 1000); // you could use .css() instead if you don't want it to animate 
+0

工作就像一個魅力:)非常感謝你:d – southpaw93