Skip to content

Instantly share code, notes, and snippets.

@cameronbaney
Created January 30, 2014 15:51
Show Gist options
  • Save cameronbaney/8711567 to your computer and use it in GitHub Desktop.
Save cameronbaney/8711567 to your computer and use it in GitHub Desktop.
Fullscreen scaling background video
var $video = $('.bg-video');
//// Videos
function setProportion(video) {
var proportion = getProportion(video);
video.width(proportion*1920);
video.height(proportion*1080);
centerVideo(video);
}
var getProportion = function(video) {
var windowWidth = video.closest('.video-parent').width();
var windowHeight = video.closest('.video-parent').height();
var windowProportion = windowWidth / windowHeight;
var origProportion = 1920 / 1080;
var proportion = windowHeight / 1080;
if (windowProportion >= origProportion) {
proportion = windowWidth / 1920;
}
return proportion;
}
var centerVideo = function(video) {
var centerX = ((video.closest('.video-parent').width() >> 1) - (video.width() >> 1)) | 0;
var centerY = (($('.video-parent').height() >> 1) - (video.height() >> 1)) | 0;
video.css({ 'left': centerX, 'top': centerY });
return;
}
// Play video
setProportion($video);
// Resize the video on window resize
if(Modernizr.video && Modernizr.touch == false){
$(window).resize(function() {
setProportion($video);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment