自动滚动聊天框

时间:2012-05-17 06:43:38

标签: php javascript jquery ajax chat

我每3秒更新一次聊天和溢出div(聊天室)。我想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 个答案:

答案 0 :(得分: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);

希望这有帮助!