我也想要一个重用html实现有播放列表的视屏播放网站
时间: 2024-03-07 09:50:14 浏览: 147
html实现视频网站(源码)
5星 · 资源好评率100%
好的,你可以尝试使用以下 HTML、CSS 和 JavaScript 代码来创建一个简单的视频播放网站,其中包含一个播放列表。
HTML 代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>视频播放网站</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<h1>视频播放网站</h1>
<div class="player-container">
<video id="player" src="video1.mp4"></video>
<div class="controls">
<button id="play-pause-btn" class="play"></button>
<input id="volume-slider" type="range" min="0" max="1" step="0.1" value="1">
<button id="mute-btn" class="unmute"></button>
<span id="current-time">00:00</span>
<input id="time-slider" type="range" min="0" step="1">
<span id="duration">00:00</span>
</div>
</div>
<ul class="playlist">
<li><a href="#" data-src="video1.mp4">视频1</a></li>
<li><a href="#" data-src="video2.mp4">视频2</a></li>
<li><a href="#" data-src="video3.mp4">视频3</a></li>
<li><a href="#" data-src="video4.mp4">视频4</a></li>
</ul>
</div>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
```
CSS 代码:
```css
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f2f2f2;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 {
text-align: center;
}
.player-container {
position: relative;
padding-top: 56.25%;
}
video {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
}
.controls {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 50px;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
}
button {
background-color: transparent;
border: none;
cursor: pointer;
outline: none;
margin: 0 10px;
}
.play:before {
content: '\25B6';
font-size: 20px;
color: #fff;
}
.pause:before {
content: '\2389';
font-size: 20px;
color: #fff;
}
.unmute:before {
content: '\1F50A';
font-size: 20px;
color: #fff;
}
.mute:before {
content: '\1F507';
font-size: 20px;
color: #fff;
}
input[type="range"] {
-webkit-appearance: none;
background-color: transparent;
height: 4px;
width: 100%;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
background-color: #fff;
border-radius: 50%;
height: 12px;
width: 12px;
cursor: pointer;
margin-top: -5px;
}
input[type="range"]::-moz-range-thumb {
background-color: #fff;
border-radius: 50%;
height: 12px;
width: 12px;
cursor: pointer;
}
.playlist {
list-style: none;
padding: 0;
margin: 20px 0 0;
display: flex;
flex-wrap: wrap;
}
.playlist li {
flex-basis: calc(25% - 20px);
margin: 10px;
}
.playlist a {
display: block;
background-color: #fff;
color: #333;
padding: 10px;
text-align: center;
text-decoration: none;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
}
.playlist a:hover {
background-color: #333;
color: #fff;
}
```
JavaScript 代码:
```javascript
var player = document.getElementById("player");
var playPauseBtn = document.getElementById("play-pause-btn");
var volumeSlider = document.getElementById("volume-slider");
var muteBtn = document.getElementById("mute-btn");
var currentTime = document.getElementById("current-time");
var timeSlider = document.getElementById("time-slider");
var duration = document.getElementById("duration");
var playlist = document.querySelectorAll(".playlist li a");
var currentVideo = 0;
// 初始化播放器
function initPlayer() {
player.volume = volumeSlider.value;
player.currentTime = 0;
currentTime.innerHTML = "00:00";
timeSlider.value = 0;
playPauseBtn.classList.remove("pause");
playPauseBtn.classList.add("play");
}
// 播放 / 暂停
function playPause() {
if (player.paused) {
player.play();
playPauseBtn.classList.remove("play");
playPauseBtn.classList.add("pause");
} else {
player.pause();
playPauseBtn.classList.remove("pause");
playPauseBtn.classList.add("play");
}
}
// 静音 / 取消静音
function muteUnmute() {
if (player.muted) {
player.muted = false;
muteBtn.classList.remove("mute");
muteBtn.classList.add("unmute");
volumeSlider.value = player.volume;
} else {
player.muted = true;
muteBtn.classList.remove("unmute");
muteBtn.classList.add("mute");
volumeSlider.value = 0;
}
}
// 调节音量
function adjustVolume() {
player.volume = volumeSlider.value;
if (player.volume === 0) {
muteBtn.classList.remove("unmute");
muteBtn.classList.add("mute");
} else {
muteBtn.classList.remove("mute");
muteBtn.classList.add("unmute");
}
}
// 更新播放时间和进度条
function updateTime() {
currentTime.innerHTML = formatTime(player.currentTime);
timeSlider.value = player.currentTime;
}
// 更新视频总时长和进度条
function updateDuration() {
duration.innerHTML = formatTime(player.duration);
timeSlider.max = player.duration;
}
// 格式化时间
function formatTime(time) {
var minutes = Math.floor(time / 60);
var seconds = Math.floor(time % 60);
return (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
}
// 跳转到指定时间点
function jumpToTime() {
player.currentTime = timeSlider.value;
}
// 切换视频
function switchVideo(event) {
event.preventDefault();
for (var i = 0; i < playlist.length; i++) {
if (playlist[i] === this) {
currentVideo = i;
player.src = playlist[i].getAttribute("data-src");
initPlayer();
playPause();
break;
}
}
}
// 初始化播放器
initPlayer();
// 绑定事件
playPauseBtn.addEventListener("click", playPause);
muteBtn.addEventListener("click", muteUnmute);
volumeSlider.addEventListener("input", adjustVolume);
player.addEventListener("timeupdate", updateTime);
player.addEventListener("loadedmetadata", updateDuration);
timeSlider.addEventListener("input", jumpToTime);
for (var i = 0; i < playlist.length; i++) {
playlist[i].addEventListener("click", switchVideo);
}
```
这个示例中包含一个播放器和一个播放列表,你可以在播放列表中添加多个视频,并且可以通过点击视频名称来切换视频。播放器具有基本的播放、暂停、音量调节、静音、进度条等功能,并且可以自适应屏幕大小。你可以根据需要进行修改和扩展。
阅读全文