1、视频播放器调节倍速
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0px;
padding: 0px;
}
.video_player{
position: relative;
width: 1000px;
height: 500px;
margin: 0px auto;
}
video{
position: absolute;
width: 1000px;
height: 500px;
left: 0px;
top: 0px;
}
.menu{
position: absolute;
width: 100%;
height: 50px;
background-color: rgba(0,0,0,0.5);
bottom: 0px;
display: none;
}
.play{
position: absolute;
width: 60px;
height: 30px;
border: 1px solid white;
text-align: center;
line-height: 30px;
color: white;
border-radius: 10px;
margin-left: 20px;
top: 50%;
margin-top: -15px;
cursor: pointer;
}
.time{
position: absolute;
width: 100px;
height: 30px;
text-align: center;
line-height: 30px;
color: white;
border-radius: 10px;
margin-left: 120px;
top: 50%;
margin-top: -15px;
cursor: pointer;
}
.progress_bar{
position: absolute;
width: 100%;
height: 2px;
background: grey;
left: 0px;
top: -2px;
}
.progress_bar div{
position: absolute;
width: 0px;
height: 2px;
background: orange;
left: 0px;
top: 0px;
}
.progress_bar i{
position: absolute;
width: 6px;
height: 6px;
border-radius: 3px;
background: white;
left: 120px;
top: -2px;
}
.quick{
position: absolute;
width: 60px;
height: 30px;
border: 1px solid white;
text-align: center;
line-height: 30px;
color: white;
border-radius: 10px;
left: 400px;
top: 50%;
margin-top: -15px;
cursor: pointer;
}
.quick_list{
position: absolute;
width: 100px;
height: 120px;
background-color: rgba(0,0,0,0.5);
left: 400px;
top: -120px;
color: white;
display: none;
}
.quick_list li{
position: relative;
width: 100%;
height: 30px;
text-align: center;
line-height: 30px;
list-style: none;
cursor: pointer;
}
.quick_list li:hover{
color: green;
}
</style>
</head>
<body>
<div class="video_player">
<!--video与audio是一样的,只比audio多一个视频,方法都一样 -->
<video src="体育视频.mp4" controls></video><!-- 加controls才能有播放栏 -->
<div class="menu">
<div class="play">播放</div>
<div class="time">0:00/0:00</div>
<div class="progress_bar">
<div></div>
<i></i>
</div>
<div class="quick">倍速</div>
<div class="quick_list">
<ul>
<li>正常</li>
<li>X1.25</li>
<li>X1.5</li>
<li>X2</li>
</ul>
</div>
</div>
</div>
<script>
var videoPlayer = document.getElementsByClassName("video_player")[0];
var video = videoPlayer.getElementsByTagName("video")[0];
var menu = document.getElementsByClassName('menu')[0];
var play = document.getElementsByClassName("play")[0];
var time = document.getElementsByClassName("time")[0];
var quick = document.getElementsByClassName("quick")[0];
var quickList = document.getElementsByClassName("quick_list")[0];
var progress_bar = document.getElementsByClassName("progress_bar")[0];
videoPlayer.onmouseenter = function() {
menu.style.display = "block";
}
videoPlayer.onmouseleave = function(){
menu.style.display = "none";
}
play.onclick = function(){
if(video.paused){
video.play();
play.innerHTML = "暂停";
}else{
video.pause();
play.innerHTML = "播放";
}
}
progress_bar.onmouseenter = function(){
progress_bar.style.height = "14px";
progress_bar.style.top = "-14px";
progress_bar.getElementsByTagName("div")[0].style.height = "14px";
progress_bar.getElementsByTagName("i")[0].style.height = "18px";
}
progress_bar.onmouseleave = function() {
progress_bar.style.height = "2px";
progress_bar.style.top = "-2px";
progress_bar.getElementsByTagName("div")[0].style.height = "2px";
progress_bar.getElementsByTagName("i")[0].style.height = "6px";
}
progress_bar.onclick = function(e){
var location = e.layerX;
var width = progress_bar.clientWidth;
var targetTime = location / width * video.duration;
video.currentTime = targetTime;
}
//调节倍速
quick.onclick=function(){
quickList.style.display = "block";
}
quickList.onmouseleave = function(){
quickList.style.display = "none";
}
var liList = quickList.getElementsByTagName("ul")[0].getElementsByTagName("li");
for(var i = 0;i < liList.length;i++){//用索引的思想,做调节速率
liList[i].index = i;
liList[i].onclick = function () {
if(this.index == 0) {//正常
video.playbackRate = 1;
quick.innerHTML = "倍速"
}else if(this.index == 1){//1.25
video.playbackRate = 1.25;
quick.innerHTML = "x1.25";
}else if(this.index == 2){//1.5
video.playbackRate = 1.5;
quick.innerHTML = "x1.5";
}else{//2
video.playbackRate = 2;
quick.innerHTML = "x2";
}
}
setInterval(function(){
var total = video.duration;//一共的时间
var nowtime = video.currentTime;//当前的时间
time.innerHTML = parseInt(nowtime / 60) + ":" + parseInt(nowtime % 60) + "/" + parseInt(total / 60) + ":" + parseInt(total % 60) ;
var width = nowtime / total * progress_bar.clientWidth;//进度条慢慢变成橙色
progress_bar.getElementsByTagName("div")[0].style.width = width + "px";
progress_bar.getElementsByTagName("i")[0].style.left = width + "px";
//Content-Range:只有http协议中带有这个属性,视频时间才能跳转
},1000)
}
</script>
</body>
</html>