2012-12-11 2 views
0

나는 div에 채팅 상자가 있습니다. 새 메시지가 도착할 때마다 메시지가 상자에 추가됩니다. 상자를 맨 아래로 자동 스크롤하여 새 메시지를 표시하려고합니다. 이것을 어떻게 할 수 있습니까?div 자동 스크롤

<div id="chatting" style="overflow: auto; background-color: #fff; border: solid 2px #dedede; width: 600px; height: 500px; padding: 10px;"> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
    hello<br/> 
</div> 
+0

[데이터를 추가 할 때 div의 끝으로 자동 스크롤하는 방법] (http://stackoverflow.com/questions/7303948/how-to-auto-scroll-to-end-of-div) -when-data-is-added) –

+0

나는이 질문을 이해하고 있는지 확신 할 수 없다. 페이지가로드 될 때 페이지를 맨 아래로 스크롤 하시겠습니까? –

답변

5

당신은 채팅 창 divscrollTop 속성을 설정하여이 작업을 수행 할 수 있습니다.

var chatWindow = $("#chating"); 
chatWindow.scrollTop(chatWindow[0].scrollHeight); 

당신은 때마다 채팅이 전화를 할 것입니다 : 또는

var chatWindow = document.getElementById('chating'); 
chatWindow.scrollTop = chatWindow.scrollHeight;​ 

http://jsfiddle.net/Ln8Hd/

사용하여 jQuery를 : 사업부의 하단으로 스크롤하려면 scrollTop 동일 scrollHeight로 설정 메시지가 div에 추가되었습니다.