html5自定義video標(biāo)簽的海報(bào)與播放按鈕功能
一、問題
1.默認(rèn)播放按鍵不好看
2.設(shè)置自定義封面圖
以上這兩點(diǎn)都想自定義
二、思路
1.使用div把video標(biāo)簽蓋住
2.div中顯示自定義的海報(bào)圖片與按鈕
三、步驟分解
1.海報(bào)圖片作為div的背景
注:無(wú)論是橫圖還是豎圖,都希望按原比例顯示在div內(nèi)
css:
.yourDiv{
background-color: black;
height:20em;
background-image: url('./images/timg.jpg');
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
html
<div class="yourDiv"></div>
效果

豎圖:

橫圖:
2.插入自定義播放圖標(biāo)
css
.yourDiv {
background-color: black;
height:20em;
background-image: url('./images/timg.jpg');
background-size: contain;
background-repeat: no-repeat;
background-position: center;
display:flex;
align-items: center;
}
.yourDiv img {
width:20%;
margin-left: 40%;
}
html
<div class="yourDiv"> <img src="./images/play.png"> </div>
效果

3.把div覆蓋到video標(biāo)簽上
css
.yourDiv {
background-color: black;
height:20em;
background-image: url('./images/timg.jpg');
background-size: contain;
background-repeat: no-repeat;
background-position: center;
display:flex;
align-items: center;
position: absolute;
top: 0px;
left: 0px;
width: 100%;
}
.yourDiv img {
width:20%;
margin-left: 40%;
}
.father {
background-color: black;
width:100%;
height:16em;
position: relative;
}
htm5
<div class="father"> <video id="video" controls="controls" autoplay="autoplay" style="width:100%;height:16em"> <source src="./videos/v1.mp4" type="video/ogg" /> <source src="./videos/v1.mp4" type="video/mp4" /> </video> <div class="yourDiv"> <img src="./images/play.png"> </div> </div>
4.觸發(fā)播放方法
html
<div class="father"> <video id="video" controls="controls" autoplay="autoplay" style="width:100%;height:16em"> <source src="./videos/v1.mp4" type="video/ogg" /> <source src="./videos/v1.mp4" type="video/mp4" /> </video> <div id="poster" class="yourDiv" onclick="play()"> <img src="./images/play.png"> </div> </div>
js
var video = document.getElementById("video");
function play(){
document.getElementById("poster").style.display = "none";
video.play();
}

這樣就達(dá)到了之前的目的
總結(jié):關(guān)鍵的還是 :background-position: center;
以上所述是小編給大家介紹的html5自定義video標(biāo)簽的海報(bào)與播放按鈕功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
html5移動(dòng)端自適應(yīng)布局的實(shí)現(xiàn)
這篇文章主要介紹了html5移動(dòng)端自適應(yīng)布局的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)2020-04-15
這篇文章主要介紹了吃透移動(dòng)端 Html5 響應(yīng)式布局,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)2019-12-16
HTML5 通過Vedio標(biāo)簽實(shí)現(xiàn)視頻循環(huán)播放的示例代碼
這篇文章主要介紹了HTML5 通過Vedio標(biāo)簽實(shí)現(xiàn)視頻循環(huán)播放的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨2020-08-05
HTML5中在title標(biāo)題標(biāo)簽里設(shè)置小圖標(biāo)的方法
這篇文章主要為大家介紹了HTML5中在title標(biāo)題標(biāo)簽里設(shè)置小圖標(biāo)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨2020-06-23
html5用video標(biāo)簽流式加載的實(shí)現(xiàn)
這篇文章主要介紹了html5用video標(biāo)簽流式加載的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)2020-05-20- 這篇文章主要介紹了詳解HTML5 Canvas標(biāo)簽及基本使用,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-10
這篇文章主要介紹了HTML5布局和HTML5標(biāo)簽的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-26




