日期:2014-05-16  浏览次数:20410 次

如何实现div内自动滚动?
做到一个聊天室,聊天信息窗口我没有用iframe或frameset,而是采用了DIV,通过CSS设置当该DIV内文本超过水平和垂直方向大小时,自动出现滚动条

现在的问题是:我不知道怎么实现自动滚动了

做了个简单的例子,代码如下:

演示地址在:   http://www.waasai.com/s/scroll.html

<!DOCTYPE   html   PUBLIC   "-//W3C//DTD   XHTML   1.0   Transitional//EN "   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html   xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 "   />
<title> 如何实现div内自动滚动? </title>
<style   type= "text/css ">
#message{width:500px;height:235px;background-color:#feeff7;overflow:scroll;overflow-x:hidden;text-overflow:ellipsis;word-break:break-all;}
#message   span{margin:6px;display:block;}
#message   span   a{color:#f60;text-decoration:underline;margin:0   4px;}
#message   span   a:hover{color:#f20;}
#message   span   label{color:#c70060;margin:0   4px;}
</style>
<script   type= "text/javascript ">
function   getEid(id){
return   document.getElementById(id);
}
function   newNode(param){
return   document.createElement(param);
}
function   newTextNode(param){
return   document.createTextNode(param);
}

function   demo(){
var   dest=getEid( "message ");
var   newStr=newTextNode(new   Date().toLocaleString()+ ":人生最大的财富是健康,最可怜的是嫉妒,最大的幸福是放得下,最大的敌人是自己!!! ");
var   span=newNode( "span ");
span.appendChild(newStr);
dest.appendChild(span);
setTimeout( "demo() ",2000);
}

window.onload=demo;
</script>
</head>

<body>
<div   id= "message "> </div>
</body>
</html>

------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
<title> 如何实现div内自动滚动? </title>
<style type= "text/css ">
#message{width:500px;height:235px;background-color:#feeff7;overflow:scroll;overflow-x:hidden;text-overflow:ellipsis;word-break:break-all;}
#message span{margin:6px;display:block;}
#message span a{color:#f60;text-decoration:underline;margin:0 4px;}
#message span a:hover{color:#f20;}
#message span label{color:#c70060;margin:0 4px;}
</style>
<script type= "text/javascript ">
function getEid(id){
return document.getElementById(id);
}
function newNode(param){
return document.createElement(param);
}
function newTextNode(param){
return document.createTextNode(param);
}

function demo(){
var dest=getEid( "message ");
var newStr=newTextNode(new Date().toLocaleString()+ ":人生最大的财富是健康,最可怜的是嫉妒,最大的幸福是放得下,最大的敌人是自己!!! ");
var span=newNode( "span ");
span.appendChild(newStr);
dest.appendChild(span);
message.scrollTop+=10000;
setTimeout( "demo() ",2000);
}

window.onload=demo;
</script>
</head>

<body>