html背景视频铺满网页,HTML – 中心全屏背景视频

这是一个JQuery函数,我写了很长一段时间才使视频成为全屏背景.基本上,如果窗口的纵横比高于视频的纵横比,则使高度100%和宽度自动,反之亦然,以获得更宽的纵横比窗口.

// 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);

});