HTML5 视频播放(video),JavaScript控制视频的实例代码

具体代码如下所示: Documenttitle> <style> <a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>caption { text-align: center; line-height: 150px; font-family: "Microsoft Yahei"; font-size: 24px; } .player { width: 720px; height: 360px; margin: 10px auto; border: 1px solid #000; background-color: #000; position: relative; border-radius: 6px; } .player video { width: 720px; height: 360px; } .controls { width: 700px; height: 40px; background-color: rgba(255,255,0.3); position: absolute; b<a href="https://www.jb51.cc/tag/ott/" target="_blank" class="keywords">ott</a>om: 10px; left: 10px; border-radius: 10px; } .switch { position: absolute; width: 22px; height: 22px; background-color: red; left: 10px; top: 9px; border-radius: 50%; } .progress { width: 432px; height: 10px; position: absolute; background-color: rgba(255,0.4); left: 40px; top: 15px; border-radius: 4px; overflow: hidden; } .curr-progress { width: 0%; height: 100%; background-color: #fff; } .time { width: 120px; height: 20px; text-align: center; line-height: 20px; font-size: 12px; color: #fff; position: absolute; left: 510px; top: 10px; } .extend { position: absolute; width: 20px; height: 20px; background-color: red; right: 10px; top: 10px; } style> head> <body> <figure> <figcaption>视频案例<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>caption> <div class="player"> <video src="11.mp4">video> <div class="controls"> <a href="#" class="switch">a> <div class="progress"> <div class="curr-progress">div> div> <div class="time"> <span class="curr-time">00:00:00span>/ <span class="total-time">00:00:00span> div> <a href="#" class="extend">a> div> div> <a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>ure> <script> var video = document.querySelector('video'); var playBtn = document.querySelector('.switch'); var currProgress = document.querySelector('.curr-progress'); var currTime = document.querySelector('.curr-time'); var totalTime = document.querySelector('.total-time'); var extend = document.querySelector('.extend'); var tTime = 0; playBtn.onclick = function() { if(video.paused){ // 如果视频是暂停的 video.play(); //play()播放 load()重新加载 pause()暂停 }else{ video.pause(); } } //当视频能播放(已经通过网络加载完成)时 video.oncanplay = function() { tTime = video.duration; //<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>视频总时长(单位秒) var tTimeStr = getTimeStr(tTime); totalTime.innerHTML = tTimeStr; } //当视频当前播放时间更新的时候 video.ontimeupdate = function() { var cTime = video.currentTime; //<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>当前播放时间 var cTimeStr = getTimeStr(cTime); console.log(cTimeStr); currTime.innerHTML = cTimeStr; currProgress.style.width = cTime/tTime*100+"%"; } extend.onclick = function() { video.webkitRequestFullScreen(); //视频全屏 } //将以秒为单位的时间变成“00:00:00”格式的字符串 function getTimeStr(time) { var h = Math.floor(time/3600); var m = Math.floor(time%3600/60); var s = Math.floor(time%60); h = h>=10?h:"0"+h; m = m>=10?m:"0"+m; s = s>=10?s:"0"+s; return h+":"+m+":"+s; } script> body> html><div class="topcard-tags"><a href="https://m.jb51.cc/tag/jsp/" class="tag_link" target="_blank">js</a><a href="https://m.jb51.cc/tag/shipin/" class="tag_link" target="_blank">视频</a><a href="https://m.jb51.cc/tag/shipinp/" class="tag_link" target="_blank">视频</a><a href="https://m.jb51.cc/tag/pshipin/" class="tag_link" target="_blank">视频</a><a href="https://m.jb51.cc/tag/shipinbofang/" class="tag_link" target="_blank">视频播放</a></div> </div> </div> </div> <!-- row end--> <div class="clearfix"></div> <!-- row --> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <div class="title"><h1>相关文章</h1></div><div class="list_con"> <img src="https://m.jb51.cc/res/2022/01-13/15/6ec3a26b3da0968fd7c5376440318f85.gif" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/html5/4673264.html" title="HTML5和CSS3实现3D展示商品信息的代码">HTML5和CSS3实现3D展示商品信息的代码</a> </div> <div class="summary">HTML5和CSS3实现3D展示商品信息的代码</div> </div><div class="list_con"> <img src="https://m.jb51.cc/res/2022/01-13/15/d3e80be331b0f15439d0abc6beb4e3bd.jpg" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/html5/4673263.html" title="利用HTML5中的Canvas绘制笑脸的代码">利用HTML5中的Canvas绘制笑脸的代码</a> </div> <div class="summary">利用HTML5中的Canvas绘制笑脸的代码</div> </div><div class="list_con"> <div class="title"> <a href="https://m.jb51.cc/html5/4673262.html" title="Html5剪切板功能的实现">Html5剪切板功能的实现</a> </div> <div class="summary">Html5剪切板功能的实现</div> </div><div class="list_con"> <div class="title"> <a href="https://m.jb51.cc/html5/4673261.html" title="如何通过HTML5触摸事件实现移动端简易进度条">如何通过HTML5触摸事件实现移动端简易进度条</a> </div> <div class="summary">如何通过HTML5触摸事件实现移动端简易进度条</div> </div><div class="list_con"> <img src="https://m.jb51.cc/res/2022/01-13/15/c2da4467d957023a84d7a28b9e996d87.gif" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/html5/4673260.html" title="Html5移动端获奖无缝滚动动画实现">Html5移动端获奖无缝滚动动画实现</a> </div> <div class="summary">Html5移动端获奖无缝滚动动画实现</div> </div><div class="list_con"> <div class="title"> <a href="https://m.jb51.cc/html5/4673259.html" title="关于HTML5和CSS3实现机器猫的代码">关于HTML5和CSS3实现机器猫的代码</a> </div> <div class="summary">关于HTML5和CSS3实现机器猫的代码</div> </div></div> </div> <!-- row end--> </div> </div> <footer id="footer"> <div class="container"> <div class="copyright"> Copyright © 2018 编程之家. 当前版本 V7.0.16<br> <span class="ml5">编程之家 版权所有 <a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">闽ICP备13020303号-8</a> </div> </div> </footer> <script> (function () { var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(bp, s); })(); </script> <script src="https://m.jb51.cc/js/count.js"></script> </body> </html>