效果还行吧,大概就不到 150 行代码即可。快点学起来吧
<!DOCTYPE html>
<html>
<head>
<title>视频弹幕</title>
<style>
* {
padding: 0;
margin: 0;
}
body {
background: rgb(245, 245, 245);
}
.container {
width: 500px;
background: #fff;
margin: 100px auto;
padding: 10px;
}
.container .top {
width: 100%;
position: relative;
overflow: hidden;
}
.container .top video {
width: 100%;
}
.container .bot {
display: flex;
}
.container .bot .danmu-num {
width: 100px;
}
.container .bot .danmu-color {
width: 80px;
}
.container .bot .danmu-input {
width: 240px;
margin-left: -15px;
}
.container .bot .danmu-input input {
width: 100%;
height: 25px;
font-size: 16px;
}
#textColor {
width: 50px;
height: 25px;
border: none;
}
.container .bot .danmu-btn {
flex: 1;
text-align: center;
background: cyan;
line-height: 25px;
cursor: pointer;
border-top-right-radius: 6px;
border-bottom-right-radius: 6px;
}
.msg {
transform: translateX(-100px);
position: absolute;
font-weight: 800;
animation: l-2-r 6s linear;
}
@keyframes l-2-r {
from {
transform: translateX(500px);
}
to {
transform: translateX(-100px);
}
}
</style>
</head>
<body>
<div class="container">
<div class="top" id="dmList">
<video src="https://video.pearvideo.com/mp4/third/20201114/cont-1707004-15488237-105621-hd.mp4" id="video" width="300" height="300"></video>
</div>
<div class="bot">
<div class="danmu-num">5678条弹幕</div>
<div class="danmu-color">
<input type="color" id="textColor">
</div>
<div class="danmu-input">
<input type="text" id="text">
</div>
<div class="danmu-btn" onclick="sendDanmu()">发送弹幕</div>
</div>
</div>
<script>
var dmList = document.getElementById('dmList')
var video = document.getElementById('video')
video.addEventListener('click', function () {
video.play()
})
function sendDanmu() {
var text = document.getElementById("text").value;
var p = document.createElement("p")
p.innerText = text;
p.setAttribute("class", "msg")
var h = Math.random() * 240;
p.style.top = h + "px"
var color = document.getElementById("textColor").value
p.style.color = color;
dmList.appendChild(p)
document.getElementById("text").value = ""
}
</script>
</body>
</html>