效果图
公用部分
html
<div id="box">
<video src="../video/film2.mp4" controls id="film" autoplay muted loop></video>
<input type="text" placeholder="发个友善的弹幕见证当下" class="dm">
<input type="button" id="btn" value="发送" class="btn" />
<!-- <span>123</span> -->
</div>
CSS
* {
margin: 0;
padding: 0;
}
#box {
width: 1000px;
height: 800px;
margin: 0 auto;
position: relative;
overflow: hidden;
}
#film {
width: 100%;
}
.dm {
box-sizing: border-box;
width: 80%;
height: 30px;
border: none;
position: absolute;
bottom: 180px;
left: 100px;
padding: 20px;
background-color: #ccc;
}
.btn {
position: absolute;
box-sizing: border-box;
width: 60px;
height: 40px;
border: none;
background-color: red;
color: white;
font-weight: 700;
bottom: 180px;
left: 900px;
}
span {
position: absolute;
white-space: nowrap;
left: 20px;
border: 2px double white;
}
</style>
不同方法实现功能
普通方法
let boxObj = document.querySelector('#box');
let filmObj = document.querySelector('#film');
let dmObj = document.querySelector('.dm');
let btnObj = document.querySelector('.btn');
let maxH = filmObj.offsetHeight;
let speed = 10;
btnObj.onclick = function () {
if (dmObj.value != '') {
move();
}
}
document.onkeydown = function (eve) {
if (eve.keyCode == 13&&dmObj.value!='') {
move();
}
}
function move() {
let maxW = boxObj.offsetWidth;
let newSpan = document.createElement('span');
newSpan.innerHTML = dmObj.value.trim();
newSpan.style.color = getColor();
newSpan.style.top = getNum(0, maxH) + 'px';
newSpan.style.left = maxW + 200 + 'px';
newSpan.style.fontSize = getNum(16, 26) + 'px';
boxObj.appendChild(newSpan);
let dmW = newSpan.offsetWidth;
console.log(dmW);
let times = setInterval(function () {
maxW -= speed;
if (maxW < (-dmW)) {
clearInterval(times);
}
newSpan.style.left = maxW + 'px';
}, 100)
dmObj.value = '';
}
function getColor() {
var a = "#";
var c = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
for (var i = 0; i < 6; i++) {
a += c[getNum(0, 15)];
}
return a;
}
function getNum(num1, num2) {
var max, min;
if (num1 > num2) {
max = num1;
min = num2;
} else {
max = num2;
min = num1;
}
return Math.round(Math.random() * (max - min) + min);
}
构造函数
function Barrage() {
this.box = document.querySelector('#box');
this.video = document.querySelector('#film');
this.dm = document.querySelector('.dm');
this.btn = document.querySelector('.btn');
this.maxH = this.video.offsetHeight;
this.maxW = this.video.offsetWidth;
this.speed = 10;
this.btn.addEventListener('click', this.clickFn.bind(this));
window.addEventListener('keydown', this.keydownFn.bind(this));
}
Barrage.prototype.clickFn = function () {
if (this.dm.value != '') {
this.move();
}
}
Barrage.prototype.keydownFn = function (eve) {
eve = eve || window.event;
if (eve.keyCode == 13 && this.dm.value != '') {
this.move();
}
}
Barrage.prototype.move = function () {
let newSpan = document.createElement('span');
newSpan.innerHTML = this.dm.value;
newSpan.style.color = this.getColor();
newSpan.style.top = this.getNum(0, this.maxH) + 'px';
newSpan.style.left = this.maxW + 'px';
newSpan.style.fontSize = this.getNum(14, 34) + 'px';
this.box.appendChild(newSpan);
let dmWidth = newSpan.offsetWidth;
let maxW = this.maxW;
let speed = this.speed;
let times = setInterval(function () {
maxW -= speed;
if (maxW < -dmWidth) {
clearInterval(times);
}
newSpan.style.left = maxW + 'px';
}, 100)
this.dm.value = '';
}
Barrage.prototype.getColor = function () {
var a = "#";
var c = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
for (var i = 0; i < 6; i++) {
a += c[this.getNum(0, 15)];
}
return a;
}
Barrage.prototype.getNum = function (num1, num2) {
var max, min;
if (num1 > num2) {
max = num1;
min = num2;
} else {
max = num2;
min = num1;
}
return Math.round(Math.random() * (max - min) + min);
}
new Barrage();
class
class Barrage {
video = document.querySelector('#film');
dm = document.querySelector('.dm');
btn = document.querySelector('.btn');
box = document.querySelector('#box');
maxH = this.video.offsetHeight;
maxW = this.video.offsetWidth;
speed = 10;
constructor() {
btn.addEventListener('click', this.clickFn.bind(this));
document.addEventListener('keydown', this.keydownFn.bind(this));
}
clickFn() {
if (this.dm.value != '') {
this.move();
}
}
keydownFn() {
if (window.event.keyCode == 13 && this.dm.value != '') {
this.move();
}
}
move() {
let newSpan = document.createElement('span');
newSpan.style.top = this.getNum(0, this.maxH) + 'px';
newSpan.style.left = this.maxW + 'px';
newSpan.style.color = this.getColor();
newSpan.style.fontSize = this.getNum(16, 36) + 'px';
newSpan.innerHTML = this.dm.value;
this.box.appendChild(newSpan);
let minLeft = newSpan.offsetWidth;
let left = this.maxW;
let speed = this.speed;
let times = setInterval(function () {
left -= speed;
if (left < -minLeft) {
clearInterval(times);
}
newSpan.style.left = left + 'px';
}, 100);
this.dm.value = '';
}
getColor() {
var a = "#";
var c = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
for (var i = 0; i < 6; i++) {
a += c[this.getNum(0, 15)];
}
return a;
}
getNum(num1, num2) {
var max, min;
if (num1 > num2) {
max = num1;
min = num2;
} else {
max = num2;
min = num1;
}
return Math.round(Math.random() * (max - min) + min);
}
}
new Barrage();