一、問題
1.默認播放按鍵不好看
2.設置自定義封面圖
以上這兩點都想自定義
二、思路
1.使用div把video標簽蓋住
2.div中顯示自定義的海報圖片與按鈕
三、步驟分解
1.海報圖片作為div的背景
注:無論是橫圖還是豎圖,都希望按原比例顯示在div內
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.插入自定義播放圖標
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標簽上
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.觸發播放方法
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();
}

這樣就達到了之前的目的
總結:關鍵的還是 :background-position: center;
以上所述是小編給大家介紹的html5自定義video標簽的海報與播放按鈕功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!