居中全屏背景视频 [英] center fullscreen background video
问题描述
我最近在玩 html5 并想到了使用的想法
I recently played around with html5 and came across the idea to use
使用以下 html 代码作为网站背景的全屏视频:
a fullscreen video as the websites' background using the following html-code:
<video id = "video_background" preload = "auto" autoplay = "true" loop = "loop">
<source src = "videos/football.mp4" type = "video/mp4" />
</video>
此外,我使用了以下 css 代码来正确对齐它:
Additionally, I used the following css-code to align it properly:
#video_background{
position: absolute;
bottom: 0px;
right: 0px;
min-width: 100%;
min-height: 100%;
max-width: 4000%;
max-height:4000%;
width: auto;
height: auto;
z-index: -1000;
overflow: hidden;
}
效果很好,但我想让我的视频水平居中在每个浏览器分辨率上.
It's working pretty well, but I would like to have my video horizontally centered on every browser resolution.
无论我尝试哪种方式来实现这种效果(通过边距或基于百分比的定位),视频一直停留在右下角.
No matter which way I tried to achieve this effect (via margin or %-based positioning), the video kept stick to the right bottom.
关于如何解决这个问题"的任何想法?
Any ideas on how to resolve this "issue"?
推荐答案
这是我很久以前写的一个 JQuery 函数,用于使视频成为全屏背景.本质上,如果窗口的纵横比高于视频的纵横比,则将高度设为 100%,宽度设为自动,反之亦然,以获取更宽的纵横比窗口.
Here's a JQuery function I wrote a long while back to make a video a fullscreen background. Essentially if the aspect ratio of the window is taller than the aspect ratio of the video then make the height 100% and width auto and vice-versa for wider aspect ratio windows.
// Resize the video elements so that we don't get any borders due to aspect ratio
function resizeVideo() {
if ($(window).height() > $(window).width() * 0.5425) { // Which dimension is bigger dependant on aspect ratio (16:9)
$("video").removeAttr("height").removeAttr("width").width("auto").height("100%");
}
else {
$("video").removeAttr("height").removeAttr("width").width("100%").height("auto");
}
};
// Add the resize function to the window resize event but put it on a short timer as to not call it too often
var resizeTimer;
$(window).resize(function () {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(resizeVideo, 150);
});
这篇关于居中全屏背景视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!