Created
June 15, 2017 07:41
-
-
Save Loac-fr/4a26d2357e175bf663998b2be8fc090b to your computer and use it in GitHub Desktop.
IE object-fit "polyfill"
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
// https://medium.com/@primozcigler/neat-trick-for-css-object-fit-fallback-on-edge-and-other-browsers-afbc53bbb2c3 | |
if ( ! Modernizr.objectfit ) { | |
$('.post__image-container').each(function () { | |
var $container = $(this), | |
imgUrl = $container.find('img').prop('src'); | |
if (imgUrl) { | |
$container | |
.css('backgroundImage', 'url(' + imgUrl + ')') | |
.addClass('compat-object-fit'); | |
} | |
}); | |
} |
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
// https://medium.com/@primozcigler/neat-trick-for-css-object-fit-fallback-on-edge-and-other-browsers-afbc53bbb2c3 | |
.post { | |
&__image-container { | |
width: 120px; // the same width and height as for the <img> | |
height: 120px; | |
&.compat-object-fit { | |
background-size: cover; | |
background-position: center center; | |
.post__featured-image { // hide image if object fit is not supported - opacity to 0 for the link area | |
opacity: 0; | |
} | |
} | |
} | |
&__featured-image { | |
width: 120px; | |
height: 120px; | |
object-fit: cover; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment