html 弹幕加流星雨背景
时间: 2023-06-11 15:09:40 浏览: 85
实现这个效果需要使用 HTML、CSS 和 JavaScript。具体步骤如下:
1. 创建一个 HTML 文件,并添加一个视频播放器和一个弹幕输入框。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>弹幕视频播放器</title>
<style>
#video-player {
width: 640px;
height: 360px;
background-color: black;
}
#danmu-input {
width: 640px;
height: 40px;
font-size: 16px;
margin-top: 10px;
}
</style>
</head>
<body>
<div id="video-player">
<video controls>
<source src="video.mp4" type="video/mp4">
</video>
</div>
<input id="danmu-input" type="text" placeholder="请输入弹幕...">
</body>
</html>
```
2. 添加弹幕样式和动画。可以使用 CSS 中的 `position`、`top`、`left` 和 `animation` 属性实现弹幕的位置和动画效果。
```css
.danmu {
position: absolute;
font-size: 16px;
white-space: nowrap;
animation: move 10s linear;
}
@keyframes move {
from {left: 100%;}
to {left: -100%;}
}
.background {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url(stars.jpg);
background-repeat: repeat;
animation: stars 10s linear;
}
@keyframes stars {
from {background-position: 0 0;}
to {background-position: 100% 100%;}
}
```
3. 使用 JavaScript 监听弹幕输入框的 `keydown` 事件,并在用户按下回车键时添加一个弹幕元素到视频播放器中。
```javascript
const videoPlayer = document.getElementById('video-player');
const danmuInput = document.getElementById('danmu-input');
danmuInput.addEventListener('keydown', (event) => {
if (event.keyCode === 13) {
const text = danmuInput.value.trim();
if (text) {
const danmu = document.createElement('div');
danmu.className = 'danmu';
danmu.innerText = text;
danmu.style.top = `${Math.floor(Math.random() * (videoPlayer.offsetHeight - 40))}px`;
videoPlayer.appendChild(danmu);
danmuInput.value = '';
}
}
});
```
4. 添加一个背景元素,并为其添加 `background` 类名,使其使用上面定义的背景样式和动画。
```html
<div class="background"></div>
```
完整的代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>弹幕视频播放器</title>
<style>
#video-player {
width: 640px;
height: 360px;
background-color: black;
}
#danmu-input {
width: 640px;
height: 40px;
font-size: 16px;
margin-top: 10px;
}
.danmu {
position: absolute;
font-size: 16px;
white-space: nowrap;
animation: move 10s linear;
}
@keyframes move {
from {left: 100%;}
to {left: -100%;}
}
.background {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url(stars.jpg);
background-repeat: repeat;
animation: stars 10s linear;
}
@keyframes stars {
from {background-position: 0 0;}
to {background-position: 100% 100%;}
}
</style>
</head>
<body>
<div class="background"></div>
<div id="video-player">
<video controls>
<source src="video.mp4" type="video/mp4">
</video>
</div>
<input id="danmu-input" type="text" placeholder="请输入弹幕...">
<script>
const videoPlayer = document.getElementById('video-player');
const danmuInput = document.getElementById('danmu-input');
danmuInput.addEventListener('keydown', (event) => {
if (event.keyCode === 13) {
const text = danmuInput.value.trim();
if (text) {
const danmu = document.createElement('div');
danmu.className = 'danmu';
danmu.innerText = text;
danmu.style.top = `${Math.floor(Math.random() * (videoPlayer.offsetHeight - 40))}px`;
videoPlayer.appendChild(danmu);
danmuInput.value = '';
}
}
});
</script>
</body>
</html>
```
请注意,这只是一个简单的示例,可能需要进一步调整和优化以实现更好的效果。
阅读全文