HTML5 視頻播放(video),JavaScript控制視頻的實(shí)例代碼
發(fā)布時間:2018-10-08 17:29:21 作者:佚名
我要評論
這篇文章主要介紹了HTML5 視頻播放(video),JavaScript控制視頻的實(shí)例代碼,需要的朋友參考下吧
具體代碼如下所示:
<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();
}
}
//當(dāng)視頻能播放(已經(jīng)通過網(wǎng)絡(luò)加載完成)時
video.oncanplay = function() {
tTime = video.duration; //獲取視頻總時長(單位秒)
var tTimeStr = getTimeStr(tTime);
totalTime.innerHTML = tTimeStr;
}
//當(dāng)視頻當(dāng)前播放時間更新的時候
video.ontimeupdate = function() {
var cTime = video.currentTime; //獲取當(dāng)前播放時間
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>
總結(jié)
以上所述是小編給大家介紹的HTML5 視頻播放(video),JavaScript控制視頻的實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Html5 video 標(biāo)簽 src 用數(shù)據(jù)流方式播放視頻
HTML5中的video標(biāo)簽用于播放視頻文件的,本文介紹了Html5 video 標(biāo)簽 src 用數(shù)據(jù)流方式播放視頻,具有一定的參考價值,感興趣的可以了解一下2023-09-27
HTML中實(shí)現(xiàn)音樂或視頻自動播放案例詳解
由于期末大作業(yè)我想插入一個背景音樂,實(shí)現(xiàn)點(diǎn)開網(wǎng)頁就會自動播放音頻的效果,今天通過本文給大家分享下我基于HTML實(shí)現(xiàn)音樂或視頻自動播放功能,代碼簡單易懂,需要的朋友參2022-05-27HTML5 video循環(huán)播放多個視頻的方法步驟
這篇文章主要介紹了HTML5 video循環(huán)播放多個視頻的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來2020-08-06- 這篇文章主要介紹了html5中嵌入視頻自動播放的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起2020-05-25
- 這篇文章主要介紹了html5自動播放mov格式視頻的實(shí)例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-14
這篇文章主要介紹了HTML5自定義視頻播放器源碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-06
目前大多數(shù)網(wǎng)絡(luò)攝像頭都是通過 RTSP 協(xié)議傳輸視頻流的,但是 HTML 并不標(biāo)準(zhǔn)支持 RTSP 流。本文重點(diǎn)給大家介紹HTML5 播放 RTSP 視頻的實(shí)例代碼,需要的朋友參考下吧2019-07-29- 這篇文章主要介紹了HTML5視頻播放插件 video.js介紹 ,需要的朋友可以參考下2018-09-29
- 這里主要研究的是通過應(yīng)用html5來解決視頻播放的問題。Adobe公司因?yàn)閼?zhàn)略錯誤,忽視了移動互聯(lián)這塊,移動終端對flash支持并不好,特別是蘋果終端都不支持flash(蘋果電腦和2016-11-06
本文主要介紹了html網(wǎng)頁播放多個視頻的幾種方法,包含iframe標(biāo)簽,VLC插件和一些常見的js插件,具有一定的參考價值,感興趣的可以了解一下2024-03-04




