2012-05-17 148 views
1

我有chatbox每3秒更新和溢出div(chatbody)。我想evry循環這個函數滾動div底部?自動滾動聊天室

我的代碼

function chat() 
{ 
var xmlhttp; 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("chatbody").innerHTML=xmlhttp.responseText; 
I THINK CODE MUST BE HERE 
    } 
    } 
    xmlhttp.open("GET","func/chat.php?tnow=" + (new Date()).getTime(),true); 
xmlhttp.send(); 
} 
setInterval("chat()",3000); 
+1

使用http://nodejs.org/進行聊天應用。它的好 – Imdad

+1

你必須設置scrollTop到chatbody相等的最後一個孩子的偏移量+它的高度。 – Sergey

+0

我想要自動滾動代碼沒有聊天web服務器js –

回答

1
function chat() 
{ 
var xmlhttp; 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("chatbody").innerHTML=xmlhttp.responseText; 
    document.getElementById("chatbody").scrollTop = document.getElementById("chatbody").scrollHeight; //This scrolls the chatbox to the bottom/last 
    } 
    } 
    xmlhttp.open("GET","func/chat.php?tnow=" + (new Date()).getTime(),true); 
xmlhttp.send(); 
} 
setInterval("chat()",3000); 

希望這有助於!