html5自定义video标签的海报与播放按钮

一、问题

  • 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

扫描二维码关注公众号,回复: 11197699 查看本文章
	<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-size: contain;
background-repeat: no-repeat;
background-position: center;;

原创文章 20 获赞 21 访问量 3万+

猜你喜欢

转载自blog.csdn.net/lanxing_huangyao/article/details/103337737
今日推荐