终极精品

标题: html5视频标签video控件自定义开发属于自己的视频播放器 [打印本页]

作者: zhoji    时间: 2018-4-7 12:42
标题: html5视频标签video控件自定义开发属于自己的视频播放器
[attach]19985[/attach]
分析:
上图是用html5的video做的视频播放器,本来是自带默认控件的,但是默认的控件太丑了,我们想要开发属于自己的UI,可以利用css3+js来实现自己需要的效果,自己做一个比默认的更好看的html5视频播放器,体验会不同哦。上图包括播放器按钮,进度条,音量控制,全屏,全部都是自定义开发的,已经隐藏掉默认的样式了。

上代码:
下面代码是html基本架构,把整个自定义控件给架构好。
  1. <body>
  2. <div id="mov">
  3. <video src="video/movie.mp4" id="video"></video><!--视频文件-->
  4. <div id="shuiyin"></div>
  5. <!--播放/暂停按钮-->
  6. <div id="ctrl">
  7. <div id="play" class="play"></div>
  8. <!--进度条-->
  9. <div id="progress">
  10. <!--进程显示-->
  11. <span id="bar">
  12. </span>
  13. <div id="control"></div><!--拖动点-->
  14. </div>
  15. <div id="sound" class="soundon"></div><!--喇叭-->
  16. <!--声音控制-->
  17. <!--控制条-->
  18. <div id="volume">
  19. <span id="volume_bar">
  20. </span>
  21. <div id="volume_control"></div><!--拖动点-->
  22. </div>
  23. <div id="full"></div>
  24. </div>
  25. </body>
复制代码
接着就是css+css3了就是美化了一下。同时把进度条结合了js来做的。
  1. <style>
  2. *{margin:0px;}
  3. #mov{width:534px; height:300px;  margin:100px auto;}
  4. #video{width:534px; height:300px; position:absolute;}
  5. #ctrl{width:534px; height:30px; background:rgba(0,0,0,0.5); position:relative; top:270px;}
  6. /*播放按钮样式*/
  7. .play{width:0px; height:0px;
  8.            border-left:12px solid #FFF;
  9.       border-top:9px solid rgba(255,255,255,0);
  10.       border-bottom:9px solid rgba(255,255,255,0); float:left; margin:6px 5px; cursor:pointer;}
  11. .play:hover{width:0px; height:0px;
  12.                 border-left:12px solid #39F;
  13.             border-top:9px solid rgba(255,255,255,0);
  14.             border-bottom:9px solid rgba(255,255,255,0); float:left; margin:6px 5px; cursor:pointer;}

  15. /*暂停按钮样式*/
  16. .pause{width:4px; height:16px; margin:7px 5px;
  17.            border-left:4px solid #FFF;
  18.            border-right:4px solid #FFF; float:left; cursor:pointer;}
  19. .pause:hover{width:4px; height:16px; margin:7px 5px;
  20.                            border-left:4px solid #39F;
  21.                           border-right:4px solid #39F; float:left; cursor:pointer;}
  22.          
  23. #progress{float:left; width:65%; height:5px;
  24.                   background:#FFF; margin:12.5px 5px; position:relative; cursor:pointer;}
  25. #bar{width:0%; height:100%; background:#39F;
  26.          display:inline-block; position:absolute; top:0; left:0; cursor:pointer;}        
  27. #control{width:9px; height:9px; position:absolute; left:-2px; top:-3px;
  28.                  border-radius:100%; background:#FFF; box-shadow:0 0 8px #3399FF;
  29.                  border:1px solid #39F; cursor:pointer;}  
  30.                  
  31. .soundon{width:16px; height:16px; background:url(images/soundon.png); position:relative; left:390px; top:8px; cursor:pointer;}
  32. .soundoff{width:16px; height:16px; background:url(images/soundoff.png); position:relative; left:390px; top:8px; cursor:pointer;}

  33. #volume{float:left; width:15%; height:5px;
  34.                 background:#FFF; margin:-3px 30px;position:relative; cursor:pointer;}
  35. #volume_bar{width:85%; height:100%; background:#39F;
  36.                 display:inline-block; position:absolute; top:0; left:0; cursor:pointer;}        
  37. #volume_control{width:9px; height:9px; position:absolute; left:60px; top:-3px;
  38.                         border-radius:100%; background:#FFF; box-shadow:0 0 8px #3399FF;
  39.                         border:1px solid #39F; cursor:pointer;}  

  40. #full{width:20px; height:20px; background:url(images/full.png); float:left;
  41.           margin-left:120px; margin-top:-11px; cursor:pointer;}  
  42. #shuiyin{width:534px; height:300px; background:url(images/shuiyin.png); position:absolute; top:100px;}
  43. </style>
复制代码
剩下的就是js了,js关系到播放暂停,进度条实时进度,拖拽,音量控制,全屏等功能。
  1. <script>
  2.         window.onload=function(){
  3.                 //ctrl对象
  4.                 var ctrl=document.getElementById("ctrl");
  5.                 //视频对象
  6.                 var video=document.getElementById("video");
  7.                 //播放按钮
  8.                 var play=document.getElementById("play");
  9.                 //进度条
  10.                 var progress=document.getElementById("progress");
  11.                 //蓝色进度条
  12.                 var bar=document.getElementById("bar");
  13.                 //控制点
  14.                 var control=document.getElementById("control");
  15.                 //喇叭
  16.                 var sound=document.getElementById("sound");
  17.                 //全屏事件
  18.                 var full=document.getElementById("full");
  19.                         full.addEventListener("click",function(){
  20.                                 video.webkitRequestFullScreen();
  21.                                 },false)
  22.                 //获取当前动作
  23.                 play.onclick=function(){
  24.                   if(video.paused){
  25.                          play.className="pause";
  26.                          video.play();
  27.                   }else{
  28.                           play.className="play";
  29.                           video.pause();
  30.                         }
  31.                   }
  32.                   //进度条跟踪事件
  33.                   video.addEventListener("timeupdate",function(){
  34.                           var scales=video.currentTime/video.duration;
  35.                           bar.style.width=progress.offsetWidth*scales+"px";
  36.                           control.style.left=progress.offsetWidth*scales+"px";
  37.                         },false);
  38.                   //进度条拖拽
  39.                   control.onmousedown=function(e){
  40.                           video.pause();
  41.                           document.onmousedown=function(e){
  42.                                  var leftv=e.clientX-progress.offsetLeft-ctrl.offsetLeft;
  43.                                  if(leftv<=0){
  44.                                          leftv=0;
  45.                                          }
  46.                                          if(leftv>=progress.offsetWidth){
  47.                                                  leftv=progress.offsetWidth;
  48.                                            }
  49.                                            control.style.left=leftv+"px"
  50.                                  }
  51.                                  document.onmouseup=function(){
  52.                                          var scales=control.offsetLeft/progress.offsetWidth;
  53.                                          video.currentTime =video.duration*scales;
  54.                                          video.play();
  55.                                          document.onmousemove=null;
  56.                                          document.onmousedown=null;
  57.                                          }
  58.                         }
  59.                 sound.onclick=function(){
  60.                         if(video.muted){
  61.                                 video.muted=false;
  62.                                 sound.className="soundon";
  63.                         }else{
  64.                                 video.muted=true;
  65.                                 sound.className="soundoff";
  66.                                 }
  67.                         }
  68.                 }
  69. </script>
复制代码







欢迎光临 终极精品 (http://www.chnspy.com/) Powered by Discuz! X3.2