我正在尝试创建一个网站,其中有一个带有一些HTML5的背景视频。 这一切都很完美地运作,它的工作方式正是我想要的。 但我还想在用户调整浏览器大小时保持图像居中。
<video id="video" src="video/video.mov" type="video/mov" autoplay loop></video>
我使用了一些jQuery使其工作,但是想知道是否有CSS解决方案。
function resizeHandler() {
// scale the video
var documentHeight = $(document).height();
var documentWidth = $(document).width();
var ratio = $('#video').width() / $('#video').height();
if((documentWidth / documentHeight) < ratio) {
$('#video').css({
'width': 'auto',
'height': '100%',
'left': '0px',
'right': '0px',
'top': '0px',
'bottom': '0px'
})
var marginRight = $('#video').width() - $(document).width();
$('#video').css('left', -marginRight);
} else {
$('#video').css({
'width': '100%',
'height': 'auto',
'left': '0px',
'right': '0px',
'top': '0px',
'bottom': '0px'
})
var marginTop = $('#video').height() - $(document).height();
$('#video').css('top', -marginTop);
}
}
以下是我编写的jQuery代码,用于拉伸图像以适应屏幕,并使图像保持居中状态。 不确定是否可以使用CSS实现,但如果有人知道如何做到这一点,那就太好了。