面向对象弹幕
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
u {
text-decoration: none;
margin-bottom: 1px;
vertical-align: top;
}
.app {
width: 800px;
height: 500px;
margin: 100px auto;
}
.app .video {
overflow: hidden;
position: relative;
width: 800px;
height: 500px;
background-color: black;
}
.app .message {
display: flex;
}
.message>input {
height: 50px;
flex: 8;
}
.message>button {
flex: 2;
}
</style>
</head>
<body>
<div class="app">
<div class="video"></div>
<div class="message">
<input type="text" name="" id="">
<button>发射弹幕</button>
</div>
</div>
<script>
var video = document.querySelector('.video');
var btn = document.querySelector('button');
var input = document.querySelector('input');
//存放弹幕的数组
var num = [];
//添加鼠标点击事件
btn.addEventListener('click', () => {
//得到input框中的值
let value = input.value;
//当input中的值为空的时候 不执行下面的函数
if (value === '') {
return
}
// console.log(1);
//创建对象 并将每次点击之后创建的对象存放在一个全局的数组中
num.push(new CreateBubble(video, value, 4))
//将input的值置为空
input.value = ''
})
function CreateBubble(container, content, great) {
//获取存放元素的播放框
this.container = container;
//获取点赞数
this.great = great;
//获取内容
this.content = content;
//创建存放弹幕的框
this.div = document.createElement('div');
//创建存放弹幕内容的框
this.span = document.createElement('span');
//创建存放点赞数的框
this.u = document.createElement('u');
//初始化函数
this.init();
//随机创建速度
this.speed = parseInt(Math.random() * 5) + 2;
//将随机创建的速度保存下来
this.speed1 = this.speed;
}
CreateBubble.prototype = {
constructor: CreateBubble,
init() {
//创建弹幕
this.createMge();
//上树
this.upTree();
//添加样式
this.addStyle();
//让弹幕移动
// this.addMove();
//停止移动
// this.stopMove();
//鼠标移入事件
this.stopMove();
//重新移动
this.reMove();
//删除事件
// this.delete();
},
//创建弹幕元素
createMge() {
//将input框中的内容给到span
this.span.innerHTML = this.content;
//将点赞数给到u
this.u.innerHTML = this.great;
//将span和u都添加到div 也就是弹幕的大框中
this.div.appendChild(this.span);
this.div.appendChild(this.u);
},
//上树
upTree() {
//将弹幕框添加到容器中
this.container.appendChild(this.div);
},
//渲染样式
addStyle() {
divStyle = {
position: 'absolute',
//给每个弹幕框一个随机高度
top: Math.random() * this.container.clientHeight / 2 + 'px',
//把弹幕框的初始位置放到容器的右边
left: this.container.clientWidth + 'px',
width: '200px',
height: '20px',
border: '1px dotted rgb(255,0,0)',
//随机弹幕框中的颜色
color: `rgb(${parseInt(Math.random()*256)},${parseInt(Math.random()*256)},${parseInt(Math.random()*256)})`,
};
for (var i in divStyle) {
this.div.style[i] = divStyle[i];
};
spanStyle = {
display: 'inline-block',
maxWidth: '180px',
// minWidth: '100px',
height: '20px',
//单行文本溢出隐藏
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
}
for (var i in spanStyle) {
// console.log(1);
this.span.style[i] = spanStyle[i];
}
},
//添加移动事件
addMove() {
//每次移动的距离都是当前距离左边框的位置减去速度
this.div.style.left = this.div.offsetLeft - this.speed + 'px';
},
//添加停止移动事件
stopMove() {
//当鼠标移入弹幕的时候 将速度置为0
this.div.addEventListener('mouseenter', () => {
this.speed = 0;
})
},
//添加重新移动事件
reMove() {
//当鼠标移出弹幕的时候 将速度置为原先的速度
this.div.addEventListener('mouseleave', () => {
this.speed = this.speed1;
})
},
//添加删除事件
delete() {
this.div.remove();
}
}
setInterval(function() {
// console.log(num);
//循环数组中的每个对象 给每个对象触发移动事件
num.forEach((item, index) => {
item.addMove();
//当弹幕的距离小于自身的宽度的负数的时候 删除元素 并将数组中对应的对象删除了
if (item.div.offsetLeft < -item.div.offsetWidth) {
num.splice(index, 1);
item.delete();
}
})
}, 30)
</script>
</body>
</html>