classs属性

poster
指定视频的占位图片
muted
视屏从一开始就静音播放
由于video元素咋各个浏览器中的样式都不一致,导致其一个浏览器一个样式,需要做样式统一,只用video播放时视频的的功能,其余功能全部自己制作;

方法属性事件

video.dur**ation
视频总时长,**以秒为单位
video.currentTi**me
视频现在播放的时长, 以秒为单位
video.play()
播放
video.pa**use()
暂停
video.current**Time = 跳转播放的位置
获取或设置跳转播放
用户单击进度条,实现跳转播放
video.playbac**kRate =倍速值
获取或设置几倍速播放
video.volum**e = 音量值
设置或获取音量
音量值为0 到 1 之间**

如何全屏播放

Element.requestFullscr**een()
是元素进入全屏模式
document.exitF**ullscreen()
是进入全屏模式的元素退出全屏模式
onfullscreenchan**ge
此事件当浏览器进入或退出全屏时触发

https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/video
https://www.w3school.com.cn/tags/html_ref_audio_video_dom.asp

demo

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. :root,
  9. body {
  10. height: 100%;
  11. }
  12. * {
  13. margin: 0;
  14. padding: 0;
  15. }
  16. ul,
  17. li {
  18. list-style: none;
  19. }
  20. .video-player {
  21. width: 1000px;
  22. height: 500px;
  23. position: relative;
  24. position: fixed;
  25. left: calc(50% - 500px);
  26. top: 10px;
  27. background: rgba(0, 0, 0, .8);
  28. }
  29. .video {
  30. width: 100%;
  31. height: 100%;
  32. position: absolute;
  33. left: 0;
  34. top: 0;
  35. }
  36. .menu {
  37. width: 100%;
  38. height: 50px;
  39. position: absolute;
  40. bottom: 0;
  41. background: rgba(0, 0, 0, .5);
  42. /* display: none; */
  43. }
  44. .btn {
  45. width: 60px;
  46. height: 30px;
  47. line-height: 30px;
  48. text-align: center;
  49. color: #fff;
  50. border-radius: 5px;
  51. border: 1px solid #fff;
  52. position: absolute;
  53. top: calc(50% - 15px);
  54. cursor: pointer;
  55. }
  56. .play {
  57. margin-left: 20px;
  58. }
  59. .time {
  60. width: 100px;
  61. border: none;
  62. margin-left: 120px;
  63. }
  64. .progress-bar {
  65. width: 1000px;
  66. height: 2px;
  67. background: gray;
  68. position: absolute;
  69. left: 0;
  70. top: -2px;
  71. }
  72. .progress-bar .progress-bar-iteam {
  73. position: absolute;
  74. width: 0px;
  75. height: 2px;
  76. left: 0;
  77. top: 0;
  78. background: white;
  79. }
  80. .progress-bar .arc {
  81. position: absolute;
  82. width: 6px;
  83. height: 6px;
  84. border-radius: 3px;
  85. background: white;
  86. left: 0px;
  87. top: -2px;
  88. }
  89. .progress-bar:hover {
  90. top: -14px;
  91. }
  92. .progress-bar:hover,
  93. .progress-bar:hover .progress-bar-iteam {
  94. height: 14px;
  95. }
  96. .progress-bar:hover .arc {
  97. height: 18px;
  98. }
  99. .quick {
  100. right: 100px;
  101. }
  102. .quick-list {
  103. position: absolute;
  104. width: 60px;
  105. height: 120px;
  106. background: rgba(0, 0, 0, .5);
  107. right: 100px;
  108. top: -120px;
  109. display: none;
  110. cursor: pointer;
  111. }
  112. .quick-list li {
  113. height: 30px;
  114. line-height: 30px;
  115. width: 100%;
  116. text-align: center;
  117. color: #fff;
  118. }
  119. .full-screen {
  120. right: 10px;
  121. }
  122. .volume {
  123. right: 190px;
  124. }
  125. .volume-bar {
  126. width: 32px;
  127. height: 100px;
  128. position: absolute;
  129. right: 204px;
  130. top: -100px;
  131. background: rgba(0, 0, 0, .8);
  132. display: none;
  133. }
  134. .volume-text {
  135. width: 100%;
  136. color: #e5e9ef;
  137. font-size: 12px;
  138. height: 28px;
  139. line-height: 28px;
  140. text-align: center;
  141. margin-bottom: 2px;
  142. outline: none;
  143. user-select: none;
  144. }
  145. .volume-iteam {
  146. width: 100%;
  147. height: 60px;
  148. cursor: pointer;
  149. position: relative;
  150. }
  151. .volume-iteam-line-white {
  152. width: 1px;
  153. height: 60px;
  154. background: #fff;
  155. margin: 0 auto;
  156. position: absolute;
  157. left: calc(50% - 0.5px);
  158. bottom: 0px;
  159. }
  160. .volume-iteam .volume-iteam-line {
  161. width: 1px;
  162. height: 30px;
  163. background: #00a1d6;
  164. position: absolute;
  165. left: 0;
  166. bottom: 0;
  167. }
  168. .volume-iteam .volume-arc {
  169. width: 12px;
  170. height: 12px;
  171. background: #00a1d6;
  172. border-radius: 50%;
  173. position: absolute;
  174. left: -6px;
  175. bottom: 24px;
  176. cursor: pointer;
  177. }
  178. </style>
  179. </head>
  180. <body>
  181. <section class="video-player">
  182. <video src="./1.mp4" class="video" poster="1.jpg"></video>
  183. <div class="menu">
  184. <div class="play btn">播放</div>
  185. <div class="time btn">00:00</div>
  186. <div class="progress-bar">
  187. <div class="progress-bar-iteam"></div>
  188. <i class="arc"></i>
  189. </div>
  190. <div class="quick btn">倍速</div>
  191. <ul class="quick-list">
  192. <li>正常</li>
  193. <li>x1.25</li>
  194. <li>x1.5</li>
  195. <li>x2</li>
  196. </ul>
  197. <div class="volume btn">音量</div>
  198. <div class="volume-bar">
  199. <p class="volume-text">100</p>
  200. <div class="volume-iteam">
  201. <div class="volume-iteam-line-white">
  202. <div class="volume-iteam-line"></div>
  203. <i class="volume-arc"></i>
  204. </div>
  205. </div>
  206. </div>
  207. <div class="full-screen btn">全屏</div>
  208. </div>
  209. </section>
  210. <script>
  211. var warp = document.getElementsByClassName('video-player')[0];
  212. var video = document.getElementsByClassName('video')[0];
  213. var menu = document.getElementsByClassName('menu')[0];
  214. var play = document.getElementsByClassName('play')[0];
  215. var time = document.getElementsByClassName('time')[0];
  216. var progressBar = document.getElementsByClassName('progress-bar')[0];
  217. var progressBarIteam = progressBar.getElementsByClassName("progress-bar-iteam")[0];
  218. var arc = progressBar.getElementsByClassName('arc')[0];
  219. var quick = document.getElementsByClassName('quick')[0];
  220. var quickList = document.getElementsByClassName('quick-list')[0];
  221. var liList = quickList.getElementsByTagName('li');
  222. var fullScreen = document.getElementsByClassName('full-screen')[0];
  223. var videoVolume = document.getElementsByClassName('volume')[0];
  224. var volumeBar = document.getElementsByClassName('volume-bar')[0];
  225. var volumeIteam = volumeBar.getElementsByClassName('volume-iteam')[0];
  226. var volumeText = volumeBar.getElementsByClassName('volume-text')[0];
  227. var volumeLine = volumeBar.getElementsByClassName('volume-iteam-line')[0];
  228. var volumeLineWhite = volumeBar.getElementsByClassName('volume-iteam-line-white')[0]
  229. var volumeArc = volumeBar.getElementsByClassName('volume-arc')[0];
  230. var timer = null;
  231. var twoClick = false;
  232. var soundVolume = 0.5;
  233. var onVolume = false;
  234. var total = '';
  235. var isFull = false;
  236. var onVolume = false;
  237. var warpIsShow = true;
  238. // 控制音量
  239. function volumeControl(num) {
  240. var soundVolume = num / 60;
  241. soundVolume > 1 ? soundVolume = 1 : soundVolume;
  242. soundVolume < 0 ? soundVolume = 0 : soundVolume;
  243. video.volume = soundVolume;
  244. console.log(soundVolume)
  245. var top = parseInt(num);
  246. var text = parseInt(soundVolume * 100)
  247. volumeText.innerHTML = text;
  248. volumeArc.style.bottom = top - 6 + 'px';
  249. volumeLine.style.height = top + 'px';
  250. }
  251. // 显示播放时间
  252. function showTime() {
  253. timer = setInterval(() => {
  254. total = video.duration;
  255. var nowTime = video.currentTime;
  256. var newtotal = (total / 60 < 10 ? "0" + parseInt(total / 60) : parseInt(total / 60)) + ":" + (
  257. total % 60 < 10 ? "0" + parseInt(total % 60) : parseInt(total % 60));
  258. var newTime = (nowTime / 60 < 10 ? "0" + parseInt(nowTime / 60) : parseInt(nowTime / 60)) +
  259. ":" + (nowTime % 60 < 10 ? "0" + parseInt(nowTime % 60) : parseInt(nowTime % 60));
  260. time.innerHTML = newTime + " / " + newtotal;
  261. var progressBarWidth = nowTime / total * progressBar.clientWidth;
  262. progressBarIteam.style.width = progressBarWidth + 'px';
  263. arc.style.left = progressBarWidth + 'px'
  264. }, 100);
  265. }
  266. showTime()
  267. // Content-Range: bytes 1966080-40909281/40909282 当在响应头有这个属性视频才能实现跳转播放
  268. function bindEvent() {
  269. warp.onmouseenter = function (e) {
  270. menu.style.display = 'block';
  271. }
  272. warp.onmouseleave = function (e) {
  273. menu.style.display = 'none'
  274. }
  275. // 用户移入进度条放大进度条
  276. // progressBar.onmouseenter = function (e) {
  277. // this.style.top = "-2px";
  278. // this.style.height = "2px";
  279. // progressBarIteam.style.height = "2px";
  280. // arc.style.height = "6px";
  281. // }
  282. // progressBar.onmouseleave = function (e) {
  283. // this.style.top = "-14px";
  284. // this.style.height = "14px";
  285. // progressBarIteam.style.height = "14px";
  286. // arc.style.height = "18px";
  287. // }
  288. // 是否播放
  289. play.onclick = function (e) {
  290. if (video.paused) {
  291. video.play()
  292. this.innerHTML = '暂停'
  293. } else {
  294. video.pause();
  295. this.innerHTML = "播放"
  296. }
  297. }
  298. // 视频跳转播放
  299. progressBar.onclick = function (e) {
  300. var locationX = e.layerX;
  301. var tagetTime = locationX / progressBar.clientWidth * total;
  302. video.currentTime = tagetTime;
  303. }
  304. // 是否显示倍速选项菜单
  305. quick.onclick = function (e) {
  306. if (!twoClick) {
  307. quickList.style.display = "block";
  308. twoClick = true;
  309. } else {
  310. quickList.style.display = 'none'
  311. twoClick = false;
  312. }
  313. }
  314. // 移出倍速选项菜单将其自身隐藏
  315. quickList.onmouseleave = function (e) {
  316. this.style.display = 'none'
  317. twoClick = false;
  318. }
  319. // 倍速播放
  320. for (var i = 0; i < liList.length; i++) {
  321. liList[i].index = i;
  322. liList[i].onclick = function (e) {
  323. if (this.index == 0) {
  324. video.playbackRate = 1
  325. quick.innerHTML = '正常'
  326. return;
  327. } else if (this.index == 1) {
  328. video.playbackRate = 1.25;
  329. quick.innerHTML = "x1.25"
  330. return;
  331. } else if (this.index == 2) {
  332. video.playbackRate = 1.5;
  333. quick.innerHTML = "x1.5"
  334. return;
  335. } else if (this.index == 3) {
  336. video.playbackRate = 2;
  337. quick.innerHTML = "x2"
  338. return;
  339. }
  340. }
  341. }
  342. // 是否全屏播放
  343. fullScreen.onclick = function (e) {
  344. if (!isFull) {
  345. // 全屏播放
  346. warp.requestFullscreen();
  347. } else {
  348. // 退出全屏
  349. document.exitFullscreen()
  350. }
  351. }
  352. // 是否进入全屏或离开全屏
  353. warp.onfullscreenchange = function () {
  354. if (isFull == false) {
  355. var disx = 0;
  356. var disy = 0;
  357. var tim = null;
  358. warpIsShow = false;
  359. // menu.style.display = "none";
  360. isFull = true;
  361. } else {
  362. isFull = false;
  363. clearInterval(tim)
  364. warpIsShow = true;
  365. }
  366. }
  367. // 是否显示调节音量的选项
  368. videoVolume.onclick = function (e) {
  369. if (!onVolume) {
  370. volumeBar.style.display = 'block';
  371. onVolume = true;
  372. } else {
  373. volumeBar.style.display = 'none';
  374. onVolume = false;
  375. }
  376. }
  377. volumeBar.onmouseleave = function (e) {
  378. this.style.display = 'none';
  379. onVolume = false;
  380. }
  381. // 单击调节音量
  382. volumeIteam.onclick = function (e) {
  383. if (e.target.className == 'volume-iteam') {
  384. var loadingY = this.clientHeight - e.layerY;
  385. volumeControl(loadingY)
  386. console.log(this.clientHeight - e.layerY)
  387. }
  388. }
  389. // 鼠标移动调节音量
  390. volumeArc.onmousedown = function (e) {
  391. volumeIteam.onmousemove = function (e) {
  392. if (e.target.className !== 'volume-arc') {
  393. var loadingY = this.clientHeight - e.layerY;
  394. volumeControl(loadingY)
  395. }
  396. // console.log(e.target.className)
  397. }
  398. document.onmouseup = function () {
  399. volumeIteam.onmousemove = null;
  400. }
  401. }
  402. }
  403. bindEvent()
  404. </script>
  405. </body>
  406. </html>