html自定义一个媒体播放器,能用音量进度条控制音量;全屏;暂停;有视频进度条控制播放进度,同时视频播放进度与进度条同步;有css样式,视频文件名为“6月4日.mp4”
时间: 2024-03-18 21:45:18 浏览: 89
带进度条的播放器(样式可以自定义)
好的,以下是一个简单的 HTML5 媒体播放器的代码,实现了您所需的功能:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>自定义媒体播放器</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #F6F6F6;
}
#video-container {
margin: 50px auto;
width: 640px;
height: 360px;
background-color: #000;
position: relative;
}
#video {
width: 100%;
height: 100%;
}
#controls {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 40px;
background-color: rgba(0,0,0,0.5);
display: flex;
flex-direction: row;
align-items: center;
padding: 0 10px;
box-sizing: border-box;
}
#play-pause-btn {
background-color: transparent;
border: none;
cursor: pointer;
margin-right: 10px;
color: #FFF;
font-size: 24px;
}
#volume-bar {
width: 100px;
height: 5px;
background-color: #FFF;
margin-right: 10px;
position: relative;
display: flex;
align-items: center;
}
#volume-bar input[type=range] {
-webkit-appearance: none;
margin: 0;
padding: 0;
width: 100%;
height: 100%;
background-color: transparent;
position: absolute;
top: 0;
left: 0;
cursor: pointer;
}
#volume-bar input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
background-color: #FFF;
width: 10px;
height: 10px;
border-radius: 50%;
margin-top: -3px;
cursor: pointer;
}
#fullscreen-btn {
background-color: transparent;
border: none;
cursor: pointer;
margin-right: 10px;
color: #FFF;
font-size: 24px;
}
#progress-bar {
width: 100%;
height: 5px;
background-color: #FFF;
position: relative;
display: flex;
align-items: center;
}
#progress-bar .progress {
position: absolute;
top: 0;
left: 0;
height: 100%;
background-color: #FF6A1E;
z-index: 1;
}
#progress-bar input[type=range] {
-webkit-appearance: none;
margin: 0;
padding: 0;
width: 100%;
height: 100%;
background-color: transparent;
position: absolute;
top: 0;
left: 0;
cursor: pointer;
}
#progress-bar input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
background-color: #FF6A1E;
width: 10px;
height: 10px;
border-radius: 50%;
margin-top: -3px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="video-container">
<video id="video" src="6月4日.mp4"></video>
<div id="controls">
<button id="play-pause-btn">►</button>
<div id="volume-bar">
<input type="range" min="0" max="1" step="0.1" value="1" id="volume-slider">
</div>
<button id="fullscreen-btn">↕</button>
<div id="progress-bar">
<div class="progress"></div>
<input type="range" min="0" max="100" step="1" value="0" id="progress-slider">
</div>
</div>
</div>
<script>
var video = document.getElementById("video");
var playPauseBtn = document.getElementById("play-pause-btn");
var volumeSlider = document.getElementById("volume-slider");
var fullscreenBtn = document.getElementById("fullscreen-btn");
var progressBar = document.getElementById("progress-bar");
var progressSlider = document.getElementById("progress-slider");
var progress = document.querySelector(".progress");
// 播放/暂停按钮
playPauseBtn.addEventListener("click", function() {
if (video.paused) {
video.play();
playPauseBtn.innerHTML = "❚❚";
} else {
video.pause();
playPauseBtn.innerHTML = "►";
}
});
// 音量滑块
volumeSlider.addEventListener("input", function() {
video.volume = volumeSlider.value;
});
// 全屏按钮
fullscreenBtn.addEventListener("click", function() {
if (video.requestFullscreen) {
video.requestFullscreen();
} else if (video.webkitRequestFullscreen) {
video.webkitRequestFullscreen();
} else if (video.mozRequestFullScreen) {
video.mozRequestFullScreen();
} else if (video.msRequestFullscreen) {
video.msRequestFullscreen();
}
});
// 进度条
video.addEventListener("timeupdate", function() {
var progressValue = (video.currentTime / video.duration) * 100;
progress.style.width = progressValue + "%";
progressSlider.value = progressValue;
});
progressSlider.addEventListener("input", function() {
video.currentTime = (progressSlider.value / 100) * video.duration;
});
</script>
</body>
</html>
```
请注意,这只是一个简单的示例,您可以根据自己的需求进行修改和定制。
阅读全文