HTML5 视频播放(video),JavaScript控制视频的实例代码
程序员文章站
2023-10-30 13:55:52
这篇文章主要介绍了HTML5 视频播放(video),JavaScript控制视频的实例代码,需要的朋友参考下吧... 18-10-08...
具体代码如下所示:
<html lang="en"> <head> <meta charset="utf-8"> <title>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,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,255,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; //获取视频总时长(单位秒) var ttimestr = gettimestr(ttime); totaltime.innerhtml = ttimestr; } //当视频当前播放时间更新的时候 video.ontimeupdate = function() { var ctime = video.currenttime; //获取当前播放时间 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>
总结
以上所述是小编给大家介绍的html5 视频播放(video),javascript控制视频的实例代码,希望对大家有所帮助
上一篇: 深入理解 JS 垃圾回收
推荐阅读
-
HTML5 视频播放(video),JavaScript控制视频的实例代码
-
HTML5重写video控制栏,Chrome去除下载按钮的代码实例讲解
-
HTML5视频播放标签video和音频播放标签audio标签的正确用法
-
录制的视频在html5网页中用video标签无法播放的问题如何解决?
-
html5 移动端视频video的android兼容(去除播放控件、全屏)
-
Springboot项目使用html5的video标签完成视频播放功能
-
vue项目中自定义video视频控制条的实现代码
-
用js来控制video播放器的代码实例教程
-
html5自动播放mov格式视频的实例代码
-
JS实现简单控制视频播放倍速的实例代码