Last active
May 18, 2016 15:04
-
-
Save r4fx/48d26b5303ec4c36f3e721ebc7acc92a to your computer and use it in GitHub Desktop.
css object-fit polyfill
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
if ('objectFit' in document.documentElement.style === false) { | |
var imgToFit = document.querySelectorAll('img.object-fit'); | |
for (var i = 0; i < imgToFit.length; i++) { | |
var container = imgToFit[i].parentElement; | |
var imgToFitUrl = imgToFit[i].src; | |
if (imgToFitUrl) { | |
container.classList.add('no-object-fit'); | |
container.style.backgroundImage = 'url(' + imgToFitUrl + ')'; | |
} | |
} | |
} |
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
.object-fit { | |
width: 100%; | |
height: 100%; | |
object-fit: cover; | |
} | |
.no-object-fit { | |
background-size: cover; | |
background-position: center center; | |
.object-fit { | |
opacity: 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment