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
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>:root,body {height: 100%;}* {margin: 0;padding: 0;}ul,li {list-style: none;}.video-player {width: 1000px;height: 500px;position: relative;position: fixed;left: calc(50% - 500px);top: 10px;background: rgba(0, 0, 0, .8);}.video {width: 100%;height: 100%;position: absolute;left: 0;top: 0;}.menu {width: 100%;height: 50px;position: absolute;bottom: 0;background: rgba(0, 0, 0, .5);/* display: none; */}.btn {width: 60px;height: 30px;line-height: 30px;text-align: center;color: #fff;border-radius: 5px;border: 1px solid #fff;position: absolute;top: calc(50% - 15px);cursor: pointer;}.play {margin-left: 20px;}.time {width: 100px;border: none;margin-left: 120px;}.progress-bar {width: 1000px;height: 2px;background: gray;position: absolute;left: 0;top: -2px;}.progress-bar .progress-bar-iteam {position: absolute;width: 0px;height: 2px;left: 0;top: 0;background: white;}.progress-bar .arc {position: absolute;width: 6px;height: 6px;border-radius: 3px;background: white;left: 0px;top: -2px;}.progress-bar:hover {top: -14px;}.progress-bar:hover,.progress-bar:hover .progress-bar-iteam {height: 14px;}.progress-bar:hover .arc {height: 18px;}.quick {right: 100px;}.quick-list {position: absolute;width: 60px;height: 120px;background: rgba(0, 0, 0, .5);right: 100px;top: -120px;display: none;cursor: pointer;}.quick-list li {height: 30px;line-height: 30px;width: 100%;text-align: center;color: #fff;}.full-screen {right: 10px;}.volume {right: 190px;}.volume-bar {width: 32px;height: 100px;position: absolute;right: 204px;top: -100px;background: rgba(0, 0, 0, .8);display: none;}.volume-text {width: 100%;color: #e5e9ef;font-size: 12px;height: 28px;line-height: 28px;text-align: center;margin-bottom: 2px;outline: none;user-select: none;}.volume-iteam {width: 100%;height: 60px;cursor: pointer;position: relative;}.volume-iteam-line-white {width: 1px;height: 60px;background: #fff;margin: 0 auto;position: absolute;left: calc(50% - 0.5px);bottom: 0px;}.volume-iteam .volume-iteam-line {width: 1px;height: 30px;background: #00a1d6;position: absolute;left: 0;bottom: 0;}.volume-iteam .volume-arc {width: 12px;height: 12px;background: #00a1d6;border-radius: 50%;position: absolute;left: -6px;bottom: 24px;cursor: pointer;}</style></head><body><section class="video-player"><video src="./1.mp4" class="video" poster="1.jpg"></video><div class="menu"><div class="play btn">播放</div><div class="time btn">00:00</div><div class="progress-bar"><div class="progress-bar-iteam"></div><i class="arc"></i></div><div class="quick btn">倍速</div><ul class="quick-list"><li>正常</li><li>x1.25</li><li>x1.5</li><li>x2</li></ul><div class="volume btn">音量</div><div class="volume-bar"><p class="volume-text">100</p><div class="volume-iteam"><div class="volume-iteam-line-white"><div class="volume-iteam-line"></div><i class="volume-arc"></i></div></div></div><div class="full-screen btn">全屏</div></div></section><script>var warp = document.getElementsByClassName('video-player')[0];var video = document.getElementsByClassName('video')[0];var menu = document.getElementsByClassName('menu')[0];var play = document.getElementsByClassName('play')[0];var time = document.getElementsByClassName('time')[0];var progressBar = document.getElementsByClassName('progress-bar')[0];var progressBarIteam = progressBar.getElementsByClassName("progress-bar-iteam")[0];var arc = progressBar.getElementsByClassName('arc')[0];var quick = document.getElementsByClassName('quick')[0];var quickList = document.getElementsByClassName('quick-list')[0];var liList = quickList.getElementsByTagName('li');var fullScreen = document.getElementsByClassName('full-screen')[0];var videoVolume = document.getElementsByClassName('volume')[0];var volumeBar = document.getElementsByClassName('volume-bar')[0];var volumeIteam = volumeBar.getElementsByClassName('volume-iteam')[0];var volumeText = volumeBar.getElementsByClassName('volume-text')[0];var volumeLine = volumeBar.getElementsByClassName('volume-iteam-line')[0];var volumeLineWhite = volumeBar.getElementsByClassName('volume-iteam-line-white')[0]var volumeArc = volumeBar.getElementsByClassName('volume-arc')[0];var timer = null;var twoClick = false;var soundVolume = 0.5;var onVolume = false;var total = '';var isFull = false;var onVolume = false;var warpIsShow = true;// 控制音量function volumeControl(num) {var soundVolume = num / 60;soundVolume > 1 ? soundVolume = 1 : soundVolume;soundVolume < 0 ? soundVolume = 0 : soundVolume;video.volume = soundVolume;console.log(soundVolume)var top = parseInt(num);var text = parseInt(soundVolume * 100)volumeText.innerHTML = text;volumeArc.style.bottom = top - 6 + 'px';volumeLine.style.height = top + 'px';}// 显示播放时间function showTime() {timer = setInterval(() => {total = video.duration;var nowTime = video.currentTime;var newtotal = (total / 60 < 10 ? "0" + parseInt(total / 60) : parseInt(total / 60)) + ":" + (total % 60 < 10 ? "0" + parseInt(total % 60) : parseInt(total % 60));var newTime = (nowTime / 60 < 10 ? "0" + parseInt(nowTime / 60) : parseInt(nowTime / 60)) +":" + (nowTime % 60 < 10 ? "0" + parseInt(nowTime % 60) : parseInt(nowTime % 60));time.innerHTML = newTime + " / " + newtotal;var progressBarWidth = nowTime / total * progressBar.clientWidth;progressBarIteam.style.width = progressBarWidth + 'px';arc.style.left = progressBarWidth + 'px'}, 100);}showTime()// Content-Range: bytes 1966080-40909281/40909282 当在响应头有这个属性视频才能实现跳转播放function bindEvent() {warp.onmouseenter = function (e) {menu.style.display = 'block';}warp.onmouseleave = function (e) {menu.style.display = 'none'}// 用户移入进度条放大进度条// progressBar.onmouseenter = function (e) {// this.style.top = "-2px";// this.style.height = "2px";// progressBarIteam.style.height = "2px";// arc.style.height = "6px";// }// progressBar.onmouseleave = function (e) {// this.style.top = "-14px";// this.style.height = "14px";// progressBarIteam.style.height = "14px";// arc.style.height = "18px";// }// 是否播放play.onclick = function (e) {if (video.paused) {video.play()this.innerHTML = '暂停'} else {video.pause();this.innerHTML = "播放"}}// 视频跳转播放progressBar.onclick = function (e) {var locationX = e.layerX;var tagetTime = locationX / progressBar.clientWidth * total;video.currentTime = tagetTime;}// 是否显示倍速选项菜单quick.onclick = function (e) {if (!twoClick) {quickList.style.display = "block";twoClick = true;} else {quickList.style.display = 'none'twoClick = false;}}// 移出倍速选项菜单将其自身隐藏quickList.onmouseleave = function (e) {this.style.display = 'none'twoClick = false;}// 倍速播放for (var i = 0; i < liList.length; i++) {liList[i].index = i;liList[i].onclick = function (e) {if (this.index == 0) {video.playbackRate = 1quick.innerHTML = '正常'return;} else if (this.index == 1) {video.playbackRate = 1.25;quick.innerHTML = "x1.25"return;} else if (this.index == 2) {video.playbackRate = 1.5;quick.innerHTML = "x1.5"return;} else if (this.index == 3) {video.playbackRate = 2;quick.innerHTML = "x2"return;}}}// 是否全屏播放fullScreen.onclick = function (e) {if (!isFull) {// 全屏播放warp.requestFullscreen();} else {// 退出全屏document.exitFullscreen()}}// 是否进入全屏或离开全屏warp.onfullscreenchange = function () {if (isFull == false) {var disx = 0;var disy = 0;var tim = null;warpIsShow = false;// menu.style.display = "none";isFull = true;} else {isFull = false;clearInterval(tim)warpIsShow = true;}}// 是否显示调节音量的选项videoVolume.onclick = function (e) {if (!onVolume) {volumeBar.style.display = 'block';onVolume = true;} else {volumeBar.style.display = 'none';onVolume = false;}}volumeBar.onmouseleave = function (e) {this.style.display = 'none';onVolume = false;}// 单击调节音量volumeIteam.onclick = function (e) {if (e.target.className == 'volume-iteam') {var loadingY = this.clientHeight - e.layerY;volumeControl(loadingY)console.log(this.clientHeight - e.layerY)}}// 鼠标移动调节音量volumeArc.onmousedown = function (e) {volumeIteam.onmousemove = function (e) {if (e.target.className !== 'volume-arc') {var loadingY = this.clientHeight - e.layerY;volumeControl(loadingY)}// console.log(e.target.className)}document.onmouseup = function () {volumeIteam.onmousemove = null;}}}bindEvent()</script></body></html>
