javascript实现弹幕

<!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:500px;
			background-color:#000;
		}
		
		#tangmu{
			width:100%;
			height:100%;
			background-color:#fff;
			z-index:9999;
			background-color: rgba(000, 1, 000, 0.1); 
		}
		
		#textStyle{
			position:absolute;
			font-size:24px;
			color:#fff;
		}
	</style>
	
 </head>

 <body>
 	<div id="play">
		<div id="tangmu">
			
		</div>
	</div>
	
	<input type="text" id="text" value="这是弹幕..."/><input type="button" value="发送" onClick="tangmu();"/>
	
	<script>
		var si;
		function tangmu(){
			clearInterval(si);
			var text = document.getElementById("text");
			var tangmu = document.getElementById("tangmu");

			var textStyle="<font id=\"textStyle\">"+text.value+"</font>";
			
			mathHeight = Math.round(Math.random()*tangmu.offsetHeight)+"px";

			var textLeft=tangmu.offsetWidth+"px";
			
			tangmu.innerHTML=textStyle;
			
			var textStyleObj = document.getElementById("textStyle");
			
			textStyleObj.style.left=textLeft;
			textStyleObj.style.top=mathHeight;
			
			var x=parseInt(textStyleObj.style.left);
			
			si = setInterval("xunhuan("+x+")",100);
			
		}
		function xunhuan(left){
			var textStyleObj = document.getElementById("textStyle");
			textStyleObj.style.left=left;
			
			var x=parseInt(textStyleObj.style.left);

			if(x<textStyleObj.style.width){
				document.getElementById("tangmu").innerHTML="";
				clearInterval(si);
			}else{
				x-=18;
			}
			
			textStyleObj.style.left=x+"px";
		}
	</script>
	
 </body>
</html>


转载于:https://my.oschina.net/hehongbo/blog/365092

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值