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

具体代码如下所示: Documenttitle> <style> figcaption { 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; bottom: 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>视频案例figcaption> <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> figure> <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.f2er.com/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.f2er.com/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="/tag/jsp/" class="tag_link" target="_blank">js</a><a href="/tag/shipin/" class="tag_link" target="_blank">视频</a><a href="/tag/shipinp/" class="tag_link" target="_blank">视频</a><a href="/tag/pshipin/" class="tag_link" target="_blank">视频</a><a href="/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"> <div class="title"> <a href="/html5/994616.html" title="BAT大佬推荐使用的HTML5的十个功能">BAT大佬推荐使用的HTML5的十个功能</a> </div> <div class="summary">HTML5不是新事物。自从最初发布(2008年1月)以来,我们一直在使用它的一些功能。后来,我再次仔细查看...</div> </div><div class="list_con"> <div class="title"> <a href="/html5/994399.html" title="Pointer Event Api-整合鼠标事件、触摸和触控笔事件">Pointer Event Api-整合鼠标事件、触摸和触控笔事件</a> </div> <div class="summary">Pointer Events API 是Hmtl5的事件规范之一,它主要目的是用来将鼠标(Mouse)、触摸(touch)和触控笔(...</div> </div><div class="list_con"> <div class="title"> <a href="/html5/994398.html" title="CSS3图片翻转动画技术详解">CSS3图片翻转动画技术详解</a> </div> <div class="summary">CSS动画非常的有趣;这种技术的美就在于,通过使用很多简单的属性,你能创建出漂亮的消隐效果。其中代表...</div> </div><div class="list_con"> <div class="title"> <a href="/html5/994397.html" title="使用CSS3的clip-path(裁剪路径)实现剪贴区域的显示以及实例实现图片渐变">使用CSS3的clip-path(裁剪路径)实现剪贴区域的显示以及实例实现图片渐变</a> </div> <div class="summary">clip-path介绍 clip-path 直译过来就是裁剪路径,使用SVG或形状定义一个HTML元素的可见区域的方法。想象...</div> </div><div class="list_con"> <div class="title"> <a href="/html5/994396.html" title="CSS3 - @keyframes">CSS3 - @keyframes</a> </div> <div class="summary">语法 必需。动画时长的百分比。 合法的值: 0-100% from(与 0% 相同) to(与 100% 相同) 定义和用法...</div> </div><div class="list_con"> <div class="title"> <a href="/html5/994395.html" title="CSS3实现全景图特效">CSS3实现全景图特效</a> </div> <div class="summary">基本代码 html代码: 首先定义一些基本的样式和动画: background-size: auto 100%; 这段代码的意思是让...</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号-10</a> </div> </div> </footer> <script src="/js/base.js"></script> </body> </html>