弹幕
注意事项:
1.speed运动速度最好设置speed+=1,这样就可以使文字看起来匀速滑动弹幕的内容设置nowrap,如果不设置的话,当文字碰到屏幕边界,他就会不断折行,直到每一行只剩一个字
2.当弹幕距离屏幕左边为页面宽度的时候,清除span
3.弹幕的内容设置nowrap,如果不设置的话,当文字碰到屏幕边界,他就会不断折行,直到每一行只剩一个字
4.var sp=document.createElement(‘span’)要写在onclick内部,否则会出错(应该是点一次执行一次)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.box{
height: 100%;
width: 100%;
overflow: hidden;
}
.gundong{
height: 800px;
width: 100%;
background-color: white;
overflow: hidden;
}
.gundong span{
position: absolute;
}
.shuru{
height: 100px;
width: 100%;
background-color: rgb(160, 160, 158);
opacity: 0.7;
display: block;
}
.shuru input{
margin-left: 700px;
margin-top: 40px;
height: 30px;
width: 200px;
}
.shuru button{
margin-left: 20px;
height: 30px;
width: 50px;
}
.zhankai{
background-color: rgb(160, 160, 158);
opacity: 0.7;
display: none;
height: 50px;
width: 100%;
}
.zhankai button{
height: 40px;
width: 100px;
line-height: 30px;
font-size: 20px;
text-align: center;
margin-left: 700px;
margin-top: 5px;
}
</style>
</head>
<body>
<div class="box">
<div class="gundong">
</div>
<div class="shuru">
<input type="text" placeholder="点击发送弹幕" class="fasong">
<button class="btn1">发送</button>
<button class="btn2">隐藏</button>
</div>
<div class="zhankai">
<button class="btn3">展开</button>
</div>
</div>
<script>
var color=["gray","darkslategrey","dodgerblue","blue","firebrick"]
// var randomNum = Math.random()
// var minSize=10
// var maxSize=30
// var fontsize=minSize+(maxSize-minSize)*randomNum
var gundong=document.querySelector('.gundong')
var shuru=document.querySelector('.shuru')
var fasong=document.querySelector('.fasong')
var btn1=document.querySelector('.btn1')
var btn2=document.querySelector('.btn2')
var zhankai=document.querySelector('.zhankai')
var btn3=document.querySelector('.btn3')
//发送弹幕
btn1.onclick=function(){
var sp=document.createElement('span');
sp.innerHTML=fasong.value;
sp.style.whiteSpace ='nowrap'
gundong.appendChild(sp);
//颜色
sp.style.color=color[Math.floor(Math.random()*6)];
//大小
var randomNum = Math.random()
var Size=100
// var maxSize=30
var fontsize=Size*randomNum
sp.style.fontSize = fontsize + "px"
sp.style.top=(Math.random()*750)+'px'
//速度
var speed=0
var timer=setInterval(function(){
speed+=1
sp.style.left=speed+'px'
if(sp.offsetLeft>=1644){
clearInterval(timer)
gundong.removeChild(sp)
}
},0.1)
fasong.value=null
}
//隐藏
btn2.onclick=function(){
shuru.style.display="none";
zhankai.style.display="block";
}
//展开
btn3.onclick=function(){
shuru.style.display="block";
zhankai.style.display="none"
}
</script>
</body>
</html>