下面是所有html的代码包括css样式,同时根据老师讲的js内容,我做了一些注释,大家共同学习哈。
<!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=utf-8" />
<title>文字信息无缝滚动</title>
<style>
body,div,span,ul,li {
margin:0;
padding:0;
}
#mooc {
width:385px;
margin:50px auto;
border:4px solid #ddd;
border-radius:15px;
webkit-border-radius:5px;
-moz-border-radius:5px;
}
#moocTitle {
background:#C00;
height:50px;
line-height:50px;
color:#fff;
text-indent:35px;
font-size:20px;
font-weight:bold;
border-top-left-radius:10px;
webkit-border-top-left-radius:5px;
-moz-border-top-left-radius:5px;
border-top-right-radius:10px;
webkit-border-top-right-radius:5px;
-moz-border-top-right-radius:5px;
}
#moocBox {
width:335px;
height:144px;
margin:10px 0 10px 25px;
padding:0;
overflow:hidden;
font-size:12px;
}
#moocBox ul li {
list-style:none;
margin:0;
padding:0;
height:24px;
line-height:24px;
}
#moocBox ul li a {
margin:0;
padding:0;
width:180px;
float:left;
display:block;
overflow:hidden;
text-indent:15px;
color:#000;
text-decoration:none;
}
#moocBox ul li a:hover{
color:#f00;
}
#moocBox ul li span {
float:right;
color:#999;
}
</style>
</head>
<body>
<div id="mooc">
<div id="moocTitle">文字信息无缝滚动</div>
<div id="moocBox">
<ul id="con1">
<li><a href="#">1.学会html5绝对的屌丝逆袭</a><span>2013-09-18</span></li>
<li><a href="#">2.学会html5绝对的屌丝逆袭</a><span>2013-09-18</span></li>
<li><a href="#">3.学会html5绝对的屌丝逆袭</a><span>2013-09-18</span></li>
<li><a href="#">4.学会html5绝对的屌丝逆袭</a><span>2013-09-18</span></li>
<li><a href="#">5.学会html5绝对的屌丝逆袭</a><span>2013-09-18</span></li>
<li><a href="#">6.学会html5绝对的屌丝逆袭</a><span>2013-09-18</span></li>
<li><a href="#">7.学会html5绝对的屌丝逆袭</a><span>2013-09-18</span></li>
<li><a href="#">8.学会html5绝对的屌丝逆袭</a><span>2013-09-18</span></li>
<li><a href="#">9.学会html5绝对的屌丝逆袭</a><span>2013-09-18</span></li>
</ul>
<ul id="con2"><!--空的,用于克隆con1的内容-->
</ul>
</div>
</div>
<script>
var area = document.getElementById('moocBox');
var con1 = document.getElementById('con1');
var con2 = document.getElementById('con2');
con2.innerHTML = con1.innerHTML;//把con1的内容克隆给con2
function scrollUp(){
if(area.scrollTop >= area.offsetHeight){//如果area滚动的高度大于或等于area的offsetHeightn内容的高度
area.scrollTop = 0; //scrollTop向上滚动的高度,area向上滚动的初始高度是0
}else{
area.scrollTop++;
}
}
var time = 50;
var myScroll = setInterval('scrollUp()',time)
area.onmouseover = function(){
clearInterval(myScroll);//鼠标移上去清除这个动画;
}
area.onmouseout = function(){
myScroll = setInterval('scrollUp()',time);//鼠标移开继续执行这个动画;
}
</script>
</body>
</html>