# 实现网页弹幕
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Color Barrage</title>
<style>
body {
overflow: hidden;
/* 隐藏页面滚动条 */
}
.barrage {
position: absolute;
white-space: nowrap;
top: 50%;
left: 100%;
font-size: 30px;
animation: move 20s linear infinite;
}
@keyframes move {
from {
left: 100%;
}
to {
left: -100%;
}
}
</style>
</head>
<body>
<div class="container">
<div id="barrage-container"></div>
</div>
<script>
function createBarrage() {
const colors = ['#FF5733', '#33FFA8', '#3348FF', '#FF33EC', '#E8FF33']; // 颜色列表
const text = 'Random Color Barrage'; // 弹幕内容
const color = colors[Math.floor(Math.random() * colors.length)]; // 随机选择颜色
const barrage = document.createElement('div');
barrage.textContent = text;
barrage.classList.add('barrage');
barrage.style.color = color; // 应用随机颜色
document.getElementById('barrage-container').appendChild(barrage);
}
// 每隔一段时间创建一个新的弹幕
setInterval(createBarrage, 3000);
</script>
</body>
</html>