Skip to content

Instantly share code, notes, and snippets.

@jakob-e
Created November 4, 2018 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakob-e/8eb8e1c7d66e990546dae66286b45caf to your computer and use it in GitHub Desktop.
Save jakob-e/8eb8e1c7d66e990546dae66286b45caf to your computer and use it in GitHub Desktop.
object-fit for ie9+ (image only)
// 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';
}
});
}
@jakob-e
Copy link
Author

jakob-e commented Nov 4, 2018

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")});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment