1、进度条处有点难

  1. <style>
  2. *{
  3. margin: 0px;
  4. padding: 0px;
  5. }
  6. .video_player{
  7. position: relative;
  8. width: 1000px;
  9. height: 500px;
  10. margin: 0px auto;
  11. }
  12. video{
  13. position: absolute;
  14. width: 1000px;
  15. height: 500px;
  16. left: 0px;
  17. top: 0px;
  18. }
  19. .menu{
  20. position: absolute;
  21. width: 100%;
  22. height: 50px;
  23. background-color: rgba(0,0,0,0.5);
  24. bottom: 0px;
  25. display: none;
  26. }
  27. .play{
  28. position: absolute;
  29. width: 60px;
  30. height: 30px;
  31. border: 1px solid white;
  32. text-align: center;
  33. line-height: 30px;
  34. color: white;
  35. border-radius: 10px;
  36. margin-left: 20px;
  37. top: 50%;
  38. margin-top: -15px;
  39. cursor: pointer;
  40. }
  41. .time{
  42. position: absolute;
  43. width: 100px;
  44. height: 30px;
  45. text-align: center;
  46. line-height: 30px;
  47. color: white;
  48. border-radius: 10px;
  49. margin-left: 120px;
  50. top: 50%;
  51. margin-top: -15px;
  52. cursor: pointer;
  53. }
  54. .progress_bar{
  55. position: absolute;
  56. width: 100%;
  57. height: 2px;
  58. background: grey;
  59. left: 0px;
  60. top: -2px;
  61. }
  62. .progress_bar div{
  63. position: absolute;
  64. width: 0px;
  65. height: 2px;
  66. background: orange;
  67. left: 0px;
  68. top: 0px;
  69. }
  70. .progress_bar i{
  71. position: absolute;
  72. width: 6px;
  73. height: 6px;
  74. border-radius: 3px;
  75. background: white;
  76. left: 120px;
  77. top: -2px;
  78. }
  79. </style>
  80. </head>
  81. <body>
  82. <div class="video_player">
  83. <!--videoaudio是一样的,只比audio多一个视频,方法都一样 -->
  84. <video src="体育视频.mp4" controls></video><!-- controls才能有播放栏 -->
  85. <div class="menu">
  86. <div class="play">播放</div>
  87. <div class="time">000/000</div>
  88. <div class="progress_bar">
  89. <div></div>
  90. <i></i>
  91. </div>
  92. </div>
  93. </div>
  94. <script>
  95. var videoPlayer = document.getElementsByClassName("video_player")[0];
  96. var video = videoPlayer.getElementsByTagName("video")[0];
  97. var menu = document.getElementsByClassName('menu')[0];
  98. var play = document.getElementsByClassName("play")[0];
  99. var time = document.getElementsByClassName("time")[0];
  100. var progress_bar = document.getElementsByClassName("progress_bar")[0];
  101. videoPlayer.onmouseenter = function() {
  102. menu.style.display = "block";
  103. }
  104. videoPlayer.onmouseleave = function(){
  105. menu.style.display = "none";
  106. }
  107. play.onclick = function(){
  108. if(video.paused){
  109. video.play();
  110. play.innerHTML = "暂停";
  111. }else{
  112. video.pause();
  113. play.innerHTML = "播放";
  114. }
  115. }
  116. progress_bar.onmouseenter = function(){
  117. progress_bar.style.height = "14px";
  118. progress_bar.style.top = "-14px";
  119. progress_bar.getElementsByTagName("div")[0].style.height = "14px";
  120. progress_bar.getElementsByTagName("i")[0].style.height = "18px";
  121. }
  122. progress_bar.onmouseleave = function() {
  123. progress_bar.style.height = "2px";
  124. progress_bar.style.top = "-2px";
  125. progress_bar.getElementsByTagName("div")[0].style.height = "2px";
  126. progress_bar.getElementsByTagName("i")[0].style.height = "6px";
  127. }
  128. progress_bar.onclick = function(e){
  129. var location = e.layerX;
  130. var width = progress_bar.clientWidth;
  131. var targetTime = location / width * video.duration;
  132. video.currentTime = targetTime;
  133. }
  134. setInterval(function(){
  135. var total = video.duration;//一共的时间
  136. var nowtime = video.currentTime;//当前的时间
  137. time.innerHTML = parseInt(nowtime / 60) + ":" + parseInt(nowtime % 60) + "/" + parseInt(total / 60) + ":" + parseInt(total % 60) ;
  138. var width = nowtime / total * progress_bar.clientWidth;//进度条慢慢变成橙色
  139. progress_bar.getElementsByTagName("div")[0].style.width = width + "px";
  140. progress_bar.getElementsByTagName("i")[0].style.left = width + "px";
  141. //Content-Range:只有http协议中带有这个属性,视频时间才能跳转
  142. },1000)
  143. </script>
  144. </body>