Created
August 25, 2015 23:05
-
-
Save assertchris/dc25ddd6aef473029c4b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function fitToScreen(selector) { | |
var element = document.querySelector(selector); | |
var width = element.offsetWidth; | |
var height = element.offsetHeight; | |
var top = "-" + (height / 2) + "px"; | |
var left = "-" + (width / 2) + "px"; | |
var ratio = getRatio(width, height); | |
setStyles(element, { | |
"position": "absolute", | |
"left": "50%", | |
"top": "50%", | |
"margin": top + " 0 0 " + left, | |
"transform": "scale(" + ratio + ", " + ratio + ")" | |
}); | |
} | |
function getRatio(width, height) { | |
return Math.min( | |
document.body.offsetWidth / width, | |
document.body.offsetHeight / height | |
); | |
} | |
function setStyles(element, styles) { | |
for (var key in styles) { | |
if (element.style.hasOwnProperty(key)) { | |
element.style[key] = styles[key]; | |
} | |
} | |
} | |
fitToScreen(".welcome-screen"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment