效果圖: video.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>video</title> <style> *{margin:0;padding:0;list-style: none;} /* ...
效果圖:
video.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>video</title> <style> *{margin:0;padding:0;list-style: none;} /*body{background:#d0d8e9;}*/ /*要麼不加position,如果加了則必須同時設置body和height高度為100%*/ html,body{ background:#d0d8e9; position: relative; height:100%; } .box{width:540px;height:332px;box-shadow:0 0 4px #d0d8e9;position: absolute;left:50%;top:50%;margin:-166px 0 0 -270px;} .videoNode{width:540px;height:305px;/*float佈局可以清除上下間的空隙*/float:left;background-color: #000;} .ctrNode{width:540px;height:27px;/*gif格式容量更小*/background:url(data/ctrl_bg.gif) repeat-x;float:left;} .playNode{float:left;width:13px;height:15px;margin:6px 0 0 14px;/*png更清晰*/background:url(data/playbtn.png) no-repeat;cursor:pointer;} .pauseNode{float:left;width:13px;height:15px;margin:6px 0 0 14px;/*png更清晰*/background:url(data/pause.png) no-repeat;cursor:pointer;} /*時間進度條部分*/ .processNode{width:260px;height:9px;background:url(data/ctrl_bg.gif) top repeat-x;margin:9px 0 0 14px;float:left;position: relative;} .processLeft{position: absolute;left:-2px;top:0;background:url(data/proleft.png) no-repeat;width:4px;height:9px;} .processRight{position: absolute;right:-2px;top:0;background:url(data/right_bg.png) no-repeat;width:4px;height:9px;} .processCircle{position: absolute;left:-8.5px;top:-3px;background:url(data/circle.png) no-repeat;width:17px;height:17px;cursor:pointer;z-index:5;} .lineNode{width:0%;height:100%;position: absolute;top:0;left:0;background:url(data/line_bg.png) repeat-x;} .lineRight{position: absolute;width:2px;height:7px;top:0;right:0;background:url(data/line_r_bg.png) no-repeat;} /*聲音進度條部分*/ .timeNode{float:left;width:57px;height:10px;margin:9px 0 0 9px;} .timeNode span{float:left;line-height:10px;font-size:10px;color:#fff;} .volumeNode{width:19px;height:17px;float:left;margin:6px 10px 0 17px;background:url(data/volume.png) no-repeat;} .vProcessNode{width:61px;height:9px;/*background:url(data/probg.gif) top repeat-x;*/margin:9px 0 0 4px;float:left;position: relative;} .vLineNode{width:61px;height:100%;position: absolute;top:1px;left:0;background:url(data/line_bg.png) repeat-x;} .vLineRight{position: absolute;width:2px;height:7px;top:0;right:0;background:url(data/line_r_bg.png) no-repeat;} #vCircleNode{left:52px;} /*全屏部分*/ .fullNode{width:13px;height:15px;background:url(data/full.png) no-repeat;margin:6px 0 0 40px;float:left;cursor:pointer;} .fullNode:hover{transform:scale(1.1);/*transition:all .5s;*/} </style> </head> <body> <div class="box"> <video class="videoNode" src="data/imooc.mp4" poster="data/poster.jpg"></video> <div class="ctrNode"> <!-- 聲音播放 --> <div class="playNode"></div> <!-- 時間調節 --> <div class="processNode"> <div class="processLeft"></div> <div class="processRight"></div> <div class="processCircle" id="circleNode"></div> <!-- 真正的進度條 --> <div class="lineNode"> <div class="lineRight"></div> </div> </div> <!-- 時間顯示 --> <div class="timeNode"> <span class="now">00:00</span> <span>-</span> <span class="all">00:00</span> </div> <div class="volumeNode"></div> <!-- 音量調節 --> <div class="vProcessNode"> <div class="processLeft"></div> <div class="processRight"></div> <div class="processCircle" id="vCircleNode"></div> <!-- 真正的進度條 --> <div class="vLineNode"> <div class="vLineRight"></div> </div> </div> <!-- 全屏 --> <div class="fullNode"></div> </div> </div> <script> var playNode=document.getElementsByClassName("playNode")[0], videoNode=document.getElementsByClassName("videoNode")[0], fullNode=document.querySelector(".fullNode"), // 聲音顯示 nowNode=document.querySelector(".now"), allNode=document.querySelector(".all"), // 時間進度條 processNode=document.querySelector(".processNode"), lineNode=document.querySelector(".lineNode"), circleNode=document.querySelector("#circleNode"), processCircle=document.querySelector("#processCircle"), // 聲音進度條 vProcessNode=document.querySelector(".vProcessNode"), vLineNode=document.querySelector(".vLineNode"), playState=true; // 播放暫停 playNode.onclick=function(){ //es6語法 //註意:要切換的樣式一定要在初始樣式的下麵定義,否則無法進行覆蓋 //this.classList.toggle("pauseNode"); //傳統語法 playState=!playState; if(playState){ this.className="playNode"; videoNode.pause(); }else{ this.className="pauseNode"; videoNode.play(); } } //全屏 fullNode.onclick=function(){ if(videoNode.webkitRequestFullscreen){ videoNode.webkitRequestFullscreen(); }else if(videoNode.mozRequestFullScreen){ videoNode.mozRequestFullScreen(); }else{ videoNode.requestFullscreen(); } } //時間顯示(解決時間初始的NaN問題) videoNode.addEventListener("canplay",function(){ var duration=videoNode.duration; var aMin=toDou(parseInt(duration/60)); var aSec=toDou(parseInt(duration%60)); allNode.innerHTML=aMin+":"+aSec; },false); //視頻播放時,更新當前時間 videoNode.addEventListener("timeupdate",function(){ var curTime=videoNode.currentTime; var cMin=toDou(parseInt(curTime/60)); var cSec=toDou(parseInt(curTime%60)); nowNode.innerHTML=cMin+":"+cSec; //進度條運動 lineNode.style.width=(curTime/videoNode.duration*100)+"%"; circleNode.style.left=lineNode.offsetWidth-8.5+"px"; },false); //時間格式轉換 function toDou(time){ return time<10?"0"+time:time; } //拖拽進度條 circleNode.onmousedown=