视频作为网站背景?HTML 5
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3800813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
video as site background? HTML 5
提问by Roland
I want to use a video as a background instead of an image that automatically stretches to the whole screen (background).
我想使用视频作为背景,而不是自动拉伸到整个屏幕(背景)的图像。
I would also like to rotate videos and images.. so that there is a random video/image displayed in any order.
我还想旋转视频和图像......以便以任何顺序显示随机视频/图像。
It would also be nice to know how to delay video playback, so that the video only plays once 30 seconds after the site loaded.
知道如何延迟视频播放也很不错,这样视频只会在网站加载后 30 秒播放一次。
thx!
谢谢!
回答by sydlawrence
Take a look at my jquery videoBG plugin
看看我的 jquery videoBG 插件
http://syddev.com/jquery.videoBG/
http://syddev.com/jquery.videoBG/
Make any HTML5 video a site background... has an image fallback for browsers that don't support html5
将任何 HTML5 视频设为站点背景...对于不支持 html5 的浏览器具有图像回退
Really easy to use
真的很容易使用
Let me know if you need any help.
如果您需要任何帮助,请告诉我。
回答by Jey Balachandran
First, your HTML markup looks like this:
首先,您的 HTML 标记如下所示:
<video id="awesome_video" src="first_video.mp4" autoplay />
Second, your JavaScript code will look like this:
其次,您的 JavaScript 代码将如下所示:
<script type="text/javascript">
var index = 1,
playlist = ['first_video.mp4', 'second_video.mp4', 'third_video.mp4'],
video = document.getElementById('awesome_video');
video.addEventListener('ended', rotate_video, false);
function rotate_video() {
video.setAttribute('src', playlist[index]);
video.load();
index++;
if (index >= playlist.length) { index = 0; }
}
</script>
And last but not least, your CSS:
最后但并非最不重要的是,您的 CSS:
#awesome_video { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }
This will create a video element on your page that starts playing the first video right away, then iterates through the playlist defined by the JavaScript variable. Your mileage with the CSS may vary depending on the CSS for the rest of the site, but 100% width/height should do it on a basic page.
这将在您的页面上创建一个视频元素,该元素立即开始播放第一个视频,然后遍历 JavaScript 变量定义的播放列表。您对 CSS 的使用可能会因网站其余部分的 CSS 而异,但 100% 宽度/高度应该在基本页面上完成。
回答by moritz
I might have a solution for the video as background, stretched to the browser-width or height, (but the video will still preserve the aspect ratio, couldnt find a solution for that yet.):
我可能有一个将视频作为背景的解决方案,拉伸到浏览器宽度或高度,(但视频仍将保留纵横比,目前还找不到解决方案。):
Put the video right after the body-tag with style="width:100%;"
.
Right afterwords, put a "bodydummy"-tag:
将视频放在 body-tag 之后style="width:100%;"
。紧接着,放一个“bodydummy”标签:
<body>
<video id="bgVideo" autoplay poster="videos/poster.png">
<source src="videos/test-h264-640x368-highqual-winff.mp4" type="video/mp4"/>
<source src="videos/test-640x368-webmvp8-miro.webm" type="video/webm"/>
<source src="videos/test-640x368-theora-miro.ogv" type="video/ogg"/>
</video>
<img id="bgImg" src="videos/poster.png" />
<!-- This image stretches exactly to the browser width/height and lies behind the video-->
<div id="bodyDummy">
Put all your content inside the bodydummy
-div and put the z-indexes correctly in CSS like this:
将所有内容放在bodydummy
-div 中,并将 z-indexes 正确放入 CSS 中,如下所示:
#bgImg{
position: absolute;
top: 0;
left: 0;
border: 0;
z-index: 1;
width: 100%;
height: 100%;
}
#bgVideo{
position: absolute;
top: 0;
left: 0;
border: 0;
z-index: 2;
width: 100%;
height: 100%;
}
#bodyDummy{
position: absolute;
top: 0;
left: 0;
z-index: 3;
overflow: auto;
width: 100%;
height: 100%;
}
Hope I could help. Let me know when you could find a solution that the video does notmaintain the aspect ratio, so it could fill the whole browser window so we do not have to put a bgimage.
希望我能帮上忙。让我知道,当你能找到一个解决方案,该视频确实不保持纵横比,所以它可以填补整个浏览器窗口,所以我们不必把bgimage。