Created
November 4, 2018 23:39
-
-
Save jakob-e/8eb8e1c7d66e990546dae66286b45caf to your computer and use it in GitHub Desktop.
object-fit for ie9+ (image only)
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
// is object fit supported | |
if (document.body.style.objectFit === undefined) { | |
// loop through all images | |
[].slice | |
.call(document.querySelectorAll('img')) | |
.map(img => { | |
// image has background-size cover or contain | |
if(['cover', 'contain'] | |
.indexOf(getComputedStyle(img).backgroundSize) !== -1) { | |
// use src as background image | |
img.style.backgroundImage = `url(${img.src})`; | |
// replace src with transparent gif | |
img.src = 'data:;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'; | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Minified and transpiled:
void 0===document.body.style.objectFit&&[].slice.call(document.querySelectorAll("img")).map(function(A){-1!==["cover","contain"].indexOf(getComputedStyle(A).backgroundSize)&&(A.style.backgroundImage="url("+A.src+")",A.src="data:;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7")});