Last active
December 16, 2015 08:48
-
-
Save danro/5408291 to your computer and use it in GitHub Desktop.
BKWLD video background-size: cover effect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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