之前写过一篇文章来记录html把图片背景变成视频背景 《在网站建设中如何将视频设置为网页背景》 今天在给大家带来一个另一个的方法 和上次的加背景方法差不多
话不多说,先看效果图:

炫酷吗?你想实现这种动态视频作为背景的首页吗?来,一起来学习,本文将带你一起实现H5动态视频背景;
首先网上找一段清晰的视频下载下来,最好是MP4格式的;
下载好了之后我们新建一个html文件来写代码:
html代码:
-
<video id="v1" autoplay loop muted>
<source src="./video2.mp4" type="video/mp4" />
</video>
一个video标签包裹着,source代表来源文件,autoplay属性是自动播放,loop代表循环播放,muted代表无声播放;
如果不加入autoplay属性是无法自动播放的,页面将会黑屏展示;
css代码:
-
*{
margin: 0px;
padding: 0px;
}
video{
position: fixed;
right: 0px;
bottom: 0px;
min-width: 100%;
min-height: 100%;
height: auto;
width: auto;
/*加滤镜*/
/*filter: blur(15px); //背景模糊设置 */
/*-webkit-filter: grayscale(100%);*/
/*filter:grayscale(100%); //背景灰度设置*/
z-index:-11
}
source{
min-width: 100%;
min-height: 100%;
height: auto;
width: auto;
}
css代码主要是进行定位和放大达到全屏播放的效果,主要是对video进行宽高之类的设置,还有别忘了要给z-index给个值,让放置在底部,只要小于0都可以,没有影响;
就这样我们的动态视频背景就完成了,如果想设置播放速度,我们可以添加js代码(video标签加上id=“v1”属性):
-
var video= document.getElementById('v1');
video.playbackRate = 1.5;
那么如果我们想要添加内容到页面上怎么办呢?
-
<video id="v1" autoplay loop muted>
<source src="./video2.mp4" type="video/mp4" />
</video>
<h1 style="color:white">hulingweb.cn</h1>
是的,在video标签外部添加
这里把完整的HTML5实现背景视频的代码贴出来下供大家参考
-
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui" />
<meta name="format-detection" content="telephone=no" />
<title>网页视频</title>
</head>
<body>
<video id="v1" autoplay loop muted>
<source src="http://www.hyundai-ce.cn/256001.mp4" type="video/mp4" />
</video>
<style type="text/css">
*{
margin: 0px;
padding: 0px;
}
video{
position: fixed;
right: 0px;
bottom: 0px;
min-width: 100%;
min-height: 100%;
height: auto;
width: auto;
/*加滤镜*/
/*filter: blur(15px); //背景模糊设置 */
/*-webkit-filter: grayscale(100%);*/
/*filter:grayscale(100%); //背景灰度设置*/
z-index:-11
}
source{
min-width: 100%;
min-height: 100%;
height: auto;
width: auto;
}
</style>
<video id="v1" autoplay loop muted>
<source src="./video2.mp4" type="video/mp4" />
</video>
<h1 style="color:white">hulingweb.cn</h1>
<script>
//设置播放速度
var video= document.getElementById('v1');
video.playbackRate = 1.5;
</script>
</body>
</html>