HTML代码
<!DOCTYPE html>
<html>
<head>
<title>弹幕姬</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="css.css">
<script type="text/javascript" src="jquery-3.3.1.min.js"></script>
</head>
<body onselectstart="return false;">
<div class="main">
<div class="barrage"></div>
<div class="btn">
<div class="text">
<input type="text" placeholder="发一个友善的弹幕见证当下">
<div class="biu">发送</div>
</div>
<div class="color">
<span class="color1 onclick"></span>
<span class="color2"></span>
<span class="color3"></span>
<span class="color4"></span>
<span class="color5"></span>
<span class="color6"></span>
<span class="color7"></span>
<span class="color8"></span>
<span class="color9"></span>
<span class="color10"></span>
<span class="color11"></span>
<span class="color12"></span>
<span class="color13"></span>
<span class="color14"></span>
</div>
<div class="control">
<div class="kind"> 弹幕种类
<span class="clickKind">滚动</span>
<span>顶部</span>
<span>底部</span>
</div>
<div class="frequency"> 弹幕频率
<span class="clickKind">快</span>
<span>中</span>
<span>低</span>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="js.js"></script>
</body>
</html>
css
*{
padding: 0;
margin: 0;
}
body{
background-color: #03a9f4;
}
.main{
width: 800px;
height: 600px;
position: relative;
margin: 50px auto;
background-color: #000;
}
.main > div{
position: absolute;
width: 100%;
background-color: #000;
}
.barrage{
height: 80%;
overflow: hidden;
}
.barrage div{
position: absolute;
font-size: 20px;
cursor: default;
}
.myFrequency{
text-decoration: underline;
}
.btn{
height: 20%;
top: 80%;
}
.text{
position: absolute;
width: 100%;
height: 30px;
}
input{
position: absolute;
width: 596px;
height: 26px;
margin-left: 50px;
background-color: #eee;
color: #212121;
font-size: 16px;
}
.text div{
width: 60px;
height: 30px;
margin-left: 650px;
background-color: #00a1d6;
color: #fff;
font-size: 14px;
text-align: center;
line-height: 30px;
cursor: pointer;
}
.text div:hover{
filter: brightness(1.1);
}
.color{
position: absolute;
width: 350px;
height: 18px;
top: 35px;
left: 225px;
}
.color span{
display: inline-block;
height: 18px;
width: 18px;
cursor: pointer;
}
.onclick{
box-shadow: 0 0 1px 1px #fff;
}
.color1{
background-color: #FE0302;
}
.color2{
background-color: #FF7204;
}
.color3{
background-color: #FFAA02;
}
.color4{
background-color: #FFD302;
}
.color5{
background-color: #FFFF00;
}
.color6{
background-color: #A0EE00;
}
.color7{
background-color: #00CD00;
}
.color8{
background-color: #019899;
}
.color9{
background-color: #4266BE;
}
.color10{
background-color: #89D5FF;
}
.color11{
background-color: #CC0273;
}
.color12{
background-color: #222222;
}
.color13{
background-color: #9B9B9B;
}
.color14{
background-color: #FFFFFF;
}
.control{
position: absolute;
width: 600px;
height: 30px;
left: 100px;
top: 70px;
color: #fff;
font-size: 16px;
}
.control span{
display: inline-block;
width: 40px;
height: 25px;
cursor: pointer;
text-align: center;
line-height: 25px;
font-size: 16px;
background-color: #fff;
border-radius: 3px;
color: #f40;
}
.control span:hover{
background-color: orange;
color: #fff;
}
.clickKind{
background-color: orange !important;
color: #fff !important;
}
.control div{
float: left;
}
.frequency{
margin-left: 130px;
}
js
('.kind').children().length; i++) {
('.kind').children().length; j++) {
('.kind').children().eq(i).addClass('clickKind');
myFrequencykind = i;
})
}
//弹幕频率绑定事件
for (let i = 0; i < ('.frequency').children().eq(i).click(function() {
for (var j = 0; j < ('.frequency').children().eq(j).removeClass('clickKind');
}
('.color').children().length; i++) {
('.color').children().length; j++) {
('.color').children().eq(i).addClass('onclick');
myFrequencycolor = i;
})
}
//删去已经出去的弹幕
setInterval(function() {
for (var i = 0; i < ('.barrage').children().eq(i).position().left <= -('.barrage').children().eq(i).remove();
}
}
}, 100)
Interval();
('input').val() != '') {
('input').val() + '</div>');
switch (myFrequencykind) {
case 0:
myRolling();
break;
case 1:
myTopfrequency();
break;
case 2:
myBottomfrequency();
break;
}
$('input').val('');
}
})
})
//电脑产生的弹幕数组
var computerFrequency = []
//弹幕字符串数组
var frequencyArry = ['前方高能', 'prprprprpr', '2333333', '还有这种操作', '前排打call', '666666', '我们是冠军', '刀剑神域', '盘他', '冲冲冲', '报告指挥部,空降成功'];
var frequencySpeed = 100; //弹幕出现频率
var myFrequencykind = 0; //自己发射弹幕种类
var myFrequencycolor = 0; //自己发射弹幕颜色
function Interval() {
frequencyTime = setInterval(function() {
creatFrequency();
}, frequencySpeed)
}
//向下取整随机数函数[min,max)
function intRandom(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function floatRandom(min, max) {
return Math.random() * (max - min) + min;
}
function creatFrequency() {
var frequencyKind = intRandom(0, 3); //取一个随机数判断弹幕类型 0:滚动弹幕,1:顶部弹幕,2:底部弹幕
switch (frequencyKind) {
case 0:
rolling();
break;
case 1:
topFrequeny();
break;
case 2:
bottomFrequeny();
break;
}
}
//滚动弹幕函数
function rolling() {
var frequencyContent = intRandom(0, frequencyArry.length); //取一个随机数判断电脑出现什么弹幕内容
var frequencyColor = intRandom(0, 14); //滚动弹幕随机颜色
var frequencyTop = floatRandom(0, 460); //滚动弹幕取随机高度
('.barrage').children().last().css({
'top': frequencyTop + 'px',
'width': frequencyArry[frequencyContent].length * 20 + 'px',
'right': -frequencyArry[frequencyContent].length * 20 + 'px',
'height': '25px'
})
color(frequencyColor);
('.barrage').append('<div>' + frequencyArry[frequencyContent] + '</div>');
('.barrage').children().length; i++) {
if (('.barrage').children().eq(i).position().top <= 150) {
$('.barrage').children().eq(i).remove();
break;
}
}
}, 2000)
}
function bottomFrequeny() {
var frequencyContent = intRandom(0, frequencyArry.length); //取一个随机数判断电脑出现什么弹幕内容
var frequencyColor = intRandom(0, 14); //滚动弹幕随机颜色
var frequencyTop = floatRandom(300, 450); //滚动弹幕取随机高度
('.barrage').children().last().css({
'top': frequencyTop + 'px',
'width': frequencyArry[frequencyContent].length * 20 + 'px',
'left': '320px',
'height': '25px'
})
color(frequencyColor);
setTimeout(function() {
for (var i = 0; i < ('.barrage').children().eq(i).position().left == 320 && ('.barrage').children().eq(i).remove();
break;
}
}
}, 2000)
}
function color(colorKind) {
switch (colorKind) {
case 0:
('.barrage').children().last().css('color', '#FF7204');
break;
case 2:
('.barrage').children().last().css('color', '#FFD302');
break;
case 4:
('.barrage').children().last().css('color', '#A0EE00');
break;
case 6:
('.barrage').children().last().css('color', '#019899');
break;
case 8:
('.barrage').children().last().css('color', '#89D5FF');
break;
case 10:
('.barrage').children().last().css('color', '#222222');
break;
case 12:
('.barrage').children().last().css('color', '#FFFFFF');
break;
}
}
function myRolling() {
var frequencyTop = floatRandom(0, 460); //滚动弹幕取随机高度
('input').val().length * 20 + 'px',
'right': -('.barrage').children().last().animate({
'right': '800px'
}, 5000);
}
function myTopfrequency() {
var frequencyTop = floatRandom(0, 150); //滚动弹幕取随机高度
('input').val().length * 20 + 'px',
'left': '320px',
'height': '25px'
})
color(myFrequencycolor);
setTimeout(function() {
for (var i = 0; i < ('.barrage').children().eq(i).position().left == 320 && ('.barrage').children().eq(i).remove();
break;
}
}
}, 2000)
}
function myBottomfrequency() {
var frequencyTop = floatRandom(300, 450); //滚动弹幕取随机高度
('input').val().length * 20 + 'px',
'left': '320px',
'height': '25px'
})
color(myFrequencycolor);
setTimeout(function() {
for (var i = 0; i < ('.barrage').children().eq(i).position().left == 320 && ('.barrage').children().eq(i).remove();
break;
}
}
}, 2000)
}