经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » HTML5 » 查看文章
HTML5 视频播放(video),JavaScript控制视频的实例代码
来源:jb51  时间:2018/10/10 8:47:46  对本文有异议

具体代码如下所示:

 

  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Documenttitle>
  5. <style>
  6. figcaption {
  7. text-align: center;
  8. line-height: 150px;
  9. font-family: "Microsoft Yahei";
  10. font-size: 24px;
  11. }
  12. .player {
  13. width: 720px;
  14. height: 360px;
  15. margin: 10px auto;
  16. border: 1px solid #000;
  17. background-color: #000;
  18. position: relative;
  19. border-radius: 6px;
  20. }
  21. .player video {
  22. width: 720px;
  23. height: 360px;
  24. }
  25. .controls {
  26. width: 700px;
  27. height: 40px;
  28. background-color: rgba(255,255,0,0.3);
  29. position: absolute;
  30. bottom: 10px;
  31. left: 10px;
  32. border-radius: 10px;
  33. }
  34. .switch {
  35. position: absolute;
  36. width: 22px;
  37. height: 22px;
  38. background-color: red;
  39. left: 10px;
  40. top: 9px;
  41. border-radius: 50%;
  42. }
  43. .progress {
  44. width: 432px;
  45. height: 10px;
  46. position: absolute;
  47. background-color: rgba(255,255,255,0.4);
  48. left: 40px;
  49. top: 15px;
  50. border-radius: 4px;
  51. overflow: hidden;
  52. }
  53. .curr-progress {
  54. width: 0%;
  55. height: 100%;
  56. background-color: #fff;
  57. }
  58. .time {
  59. width: 120px;
  60. height: 20px;
  61. text-align: center;
  62. line-height: 20px;
  63. font-size: 12px;
  64. color: #fff;
  65. position: absolute;
  66. left: 510px;
  67. top: 10px;
  68. }
  69. .extend {
  70. position: absolute;
  71. width: 20px;
  72. height: 20px;
  73. background-color: red;
  74. right: 10px;
  75. top: 10px;
  76. }
  77. style>
  78. head>
  79. <body>
  80. <figure>
  81. <figcaption>视频案例figcaption>
  82. <div class="player">
  83. <video src="11.mp4">video>
  84. <div class="controls">
  85. <a href="#" class="switch">a>
  86. <div class="progress">
  87. <div class="curr-progress">div>
  88. div>
  89. <div class="time">
  90. <span class="curr-time">00:00:00span>/
  91. <span class="total-time">00:00:00span>
  92. div>
  93. <a href="#" class="extend">a>
  94. div>
  95. div>
  96. figure>
  97. <script>
  98. var video = document.querySelector('video');
  99. var playBtn = document.querySelector('.switch');
  100. var currProgress = document.querySelector('.curr-progress');
  101. var currTime = document.querySelector('.curr-time');
  102. var totalTime = document.querySelector('.total-time');
  103. var extend = document.querySelector('.extend');
  104. var tTime = 0;
  105. playBtn.onclick = function() {
  106. if(video.paused){ // 如果视频是暂停的
  107. video.play(); //play()播放 load()重新加载 pause()暂停
  108. }else{
  109. video.pause();
  110. }
  111. }
  112. //当视频能播放(已经通过网络加载完成)时
  113. video.oncanplay = function() {
  114. tTime = video.duration; //获取视频总时长(单位秒)
  115. var tTimeStr = getTimeStr(tTime);
  116. totalTime.innerHTML = tTimeStr;
  117. }
  118. //当视频当前播放时间更新的时候
  119. video.ontimeupdate = function() {
  120. var cTime = video.currentTime; //获取当前播放时间
  121. var cTimeStr = getTimeStr(cTime);
  122. console.log(cTimeStr);
  123. currTime.innerHTML = cTimeStr;
  124. currProgress.style.width = cTime/tTime*100+"%";
  125. }
  126. extend.onclick = function() {
  127. video.webkitRequestFullScreen(); //视频全屏
  128. }
  129. //将以秒为单位的时间变成“00:00:00”格式的字符串
  130. function getTimeStr(time) {
  131. var h = Math.floor(time/3600);
  132. var m = Math.floor(time%3600/60);
  133. var s = Math.floor(time%60);
  134. h = h>=10?h:"0"+h;
  135. m = m>=10?m:"0"+m;
  136. s = s>=10?s:"0"+s;
  137. return h+":"+m+":"+s;
  138. }
  139. script>
  140. body>
  141. html>

总结

以上所述是小编给大家介绍的HTML5 视频播放(video),JavaScript控制视频的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对w3xue网站的支持!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号