Skip to content

Instantly share code, notes, and snippets.

@danro
Last active December 16, 2015 08:48
Show Gist options
  • Save danro/5408291 to your computer and use it in GitHub Desktop.
Save danro/5408291 to your computer and use it in GitHub Desktop.
BKWLD video background-size: cover effect
// Depends on jQuery
// Assumes outerDiv and videoTag are jQuery objects
var width = outerDiv.width();
var height = outerDiv.height();
var aspectW = 16;
var aspectH = 9;
var scaleX = width / aspectW;
var scaleY = height / aspectH;
var scale = Math.max(scaleX, scaleY);
var w = Math.ceil(aspectW * scale);
var h = Math.ceil(aspectH * scale);
var x = 0;
var y = 0;
if (w > width) x = -(w - width) * 0.5;
if (h > height) y = -(h - height) * 0.5;
videoTag.css({
width: w,
height: h,
top: y,
left: x
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment