1、进度条处有点难
<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;
}
</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>
</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 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;
}
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>