使用html做一个类似抖音的视频播放网页,在例子中包含多个视频,包含视频暂停,快进等功能
时间: 2023-09-21 20:08:14 浏览: 183
好的,以下是一个简单的示例代码,实现了一个类似抖音的视频播放网页,包含多个视频,视频暂停和快进功能。
```html
<!DOCTYPE html>
<html>
<head>
<title>抖音视频播放网页</title>
<meta charset="UTF-8">
<style>
#video-container {
display: flex;
flex-wrap: wrap;
}
.video {
width: 320px;
height: 568px;
margin: 10px;
position: relative;
}
.video video {
width: 100%;
height: 100%;
object-fit: cover;
}
.play-btn {
width: 60px;
height: 60px;
background-color: rgba(0, 0, 0, 0.5);
border-radius: 50%;
position: absolute;
top: calc(50% - 30px);
left: calc(50% - 30px);
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
.play-btn:hover {
background-color: rgba(0, 0, 0, 0.8);
}
.play-btn i {
font-size: 30px;
color: #fff;
}
.control-bar {
display: flex;
align-items: center;
justify-content: space-between;
background-color: rgba(0, 0, 0, 0.5);
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 40px;
padding: 0 10px;
box-sizing: border-box;
visibility: hidden;
}
.video:hover .control-bar {
visibility: visible;
}
.control-bar .play-pause-btn {
width: 30px;
height: 30px;
background-color: transparent;
border: none;
cursor: pointer;
}
.control-bar .play-pause-btn i {
font-size: 20px;
color: #fff;
}
.control-bar .progress-bar {
width: 60%;
height: 5px;
background-color: rgba(255, 255, 255, 0.5);
border-radius: 2.5px;
position: relative;
margin: 0 10px;
cursor: pointer;
}
.progress-bar .progress {
width: 0;
height: 100%;
background-color: #fff;
border-radius: 2.5px;
position: absolute;
left: 0;
top: 0;
}
.control-bar .time {
font-size: 12px;
color: #fff;
}
</style>
</head>
<body>
<div id="video-container">
<div class="video">
<video src="video1.mp4"></video>
<div class="play-btn">
<i class="fas fa-play"></i>
</div>
<div class="control-bar">
<button class="play-pause-btn"><i class="fas fa-pause"></i></button>
<div class="progress-bar">
<div class="progress"></div>
</div>
<div class="time">00:00 / 00:00</div>
</div>
</div>
<div class="video">
<video src="video2.mp4"></video>
<div class="play-btn">
<i class="fas fa-play"></i>
</div>
<div class="control-bar">
<button class="play-pause-btn"><i class="fas fa-pause"></i></button>
<div class="progress-bar">
<div class="progress"></div>
</div>
<div class="time">00:00 / 00:00</div>
</div>
</div>
<div class="video">
<video src="video3.mp4"></video>
<div class="play-btn">
<i class="fas fa-play"></i>
</div>
<div class="control-bar">
<button class="play-pause-btn"><i class="fas fa-pause"></i></button>
<div class="progress-bar">
<div class="progress"></div>
</div>
<div class="time">00:00 / 00:00</div>
</div>
</div>
</div>
<script src="https://kit.fontawesome.com/4cc07f8c7f.js" crossorigin="anonymous"></script>
<script>
const videos = document.querySelectorAll('video');
const playBtns = document.querySelectorAll('.play-btn');
const playPauseBtns = document.querySelectorAll('.play-pause-btn');
const progressBars = document.querySelectorAll('.progress-bar');
const progresses = document.querySelectorAll('.progress');
const times = document.querySelectorAll('.time');
videos.forEach((video, index) => {
// 播放按钮事件
playBtns[index].addEventListener('click', () => {
video.play();
playBtns[index].style.display = 'none';
});
// 暂停和播放按钮事件
playPauseBtns[index].addEventListener('click', () => {
if (video.paused) {
video.play();
playPauseBtns[index].innerHTML = '<i class="fas fa-pause"></i>';
} else {
video.pause();
playPauseBtns[index].innerHTML = '<i class="fas fa-play"></i>';
}
});
// 进度条事件
progressBars[index].addEventListener('click', (event) => {
const width = progressBars[index].clientWidth;
const clickX = event.offsetX;
const duration = video.duration;
const currentTime = (clickX / width) * duration;
video.currentTime = currentTime;
});
// 视频播放事件
video.addEventListener('timeupdate', () => {
const currentTime = video.currentTime;
const duration = video.duration;
const progressWidth = (currentTime / duration) * 100;
progresses[index].style.width = `${progressWidth}%`;
// 时间显示
const currentMinutes = Math.floor(currentTime / 60);
const currentSeconds = Math.floor(currentTime % 60);
const durationMinutes = Math.floor(duration / 60);
const durationSeconds = Math.floor(duration % 60);
times[index].textContent = `${currentMinutes}:${currentSeconds.toString().padStart(2, '0')} / ${durationMinutes}:${durationSeconds.toString().padStart(2, '0')}`;
});
// 视频结束事件
video.addEventListener('ended', () => {
playBtns[index].style.display = 'flex';
playPauseBtns[index].innerHTML = '<i class="fas fa-play"></i>';
progresses[index].style.width = '0';
times[index].textContent = '00:00 / 00:00';
});
});
</script>
</body>
</html>
```
以上代码使用了 HTML、CSS 和 JavaScript 实现了一个简单的视频播放网页,包含了多个视频、视频暂停和快进功能。你可以将视频文件替换为自己的视频文件,然后在浏览器中打开该网页进行测试。
阅读全文