<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>javascript弹幕</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style>
#play{
width:600px;
height:200px;
background-color:#000;
}
#tangmu{
width:600px;
height:200px;
background-color:#fff;
z-index:9999;
background-color: rgba(000, 1, 000, 0.1);
}
.tmf{
position:absolute;
font-size:24px;
color:#fff;
height:24px;
}
</style>
</head>
<body>
<br />
<br />
<br />
<br />
<br />
<div id="play">
<div id="tangmu">
<font class="tmf" id="t1" >檀木</font>
<font class="tmf" id="t2">红米</font>
<font class="tmf" id="t3" >大妈</font>
<font class="tmf" id="Font1" >檀木</font>
<font class="tmf" id="Font2">红米</font>
<font class="tmf" id="Font3" >大妈</font>
<font class="tmf" id="Font4" >檀木</font>
<font class="tmf" id="Font5">红米</font>
<font class="tmf" id="Font6" >大妈</font>
<font class="tmf" id="Font7" >檀木</font>
<font class="tmf" id="Font8">红米</font>
<font class="tmf" id="Font9" >大妈</font>
<font class="tmf" id="Font10" >檀木</font>
<font class="tmf" id="Font11">红米</font>
<font class="tmf" id="Font12" >大妈</font>
<font class="tmf" id="Font13" >檀木</font>
<font class="tmf" id="Font14">红米</font>
<font class="tmf" id="Font15" >大妈</font>
<font class="tmf" id="Font16" >檀木</font>
<font class="tmf" id="Font17">红米</font>
<font class="tmf" id="Font18" >大妈</font>
<font class="tmf" id="Font19" >檀木</font>
<font class="tmf" id="Font20">红米</font>
<font class="tmf" id="Font21" >大妈</font>
<font class="tmf" id="Font22" >檀木</font>
<font class="tmf" id="Font23">红米</font>
<font class="tmf" id="Font24" >大妈</font>
</div>
</div>
<script src="js/jquery-1.7.2.min.js"></script>
<script>
var times = 4000;//时间,定义速度
var hdjgtime = 500;//滑动间隔
var jgtime = 0;
var tmu = 1;
var fjid = "tangmu";//夫级id
var itemclass = "tmf";//子集class
function tm() {
times = 5000;
jgtime = 1000;
jgtime = 0;
$(".tmf").each(function () {
tangmu($(this).attr("id"));
});
}
tm();//页面初始调用
function tangmu(id){
var tangmu = document.getElementById(fjid);
var mathHeight = Math.round(Math.random()*(tangmu.offsetHeight))+"px";
var textLeft=tangmu.offsetWidth+"px";
var textStyleObj = document.getElementById(id);
textStyleObj.style.marginLeft = textLeft;
if (Number(mathHeight.replace("px","")) > (tangmu.offsetHeight - textStyleObj.offsetHeight)) {
mathHeight = (tangmu.offsetHeight - textStyleObj.offsetHeight)+"px";
}
textStyleObj.style.marginTop = mathHeight;
setTimeout(function () {
if ($("#" + id).index() == $("." + itemclass).length - 1)
{ $("#" + id).animate({ "margin-left": "-100px" }, times, function () { tm(); }); }
else
{ $("#" + id).animate({ "margin-left": "-100px" }, times); }
}, jgtime);
var tisj= Math.floor(Math.random() * 10 + 1);
jgtime = jgtime + hdjgtime;//每个追加一秒
}
</script>
</body>
</html>