Last active
October 1, 2019 15:53
-
-
Save AndreKelling/fbf8ba53c5492a8c51220516e4eb0043 to your computer and use it in GitHub Desktop.
CSS object-fit to background-image fallback // IE11
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
// Detect objectFit support | |
// set full size img to selected container as background | |
// especially for IE11 | |
module.exports = () => { | |
if ('objectFit' in document.documentElement.style === false) { | |
// assign HTMLCollection with parents of images with objectFit to variable | |
const container = document.getElementsByClassName('js-sliderImg'); | |
// Loop through HTMLCollection | |
for (let i = 0; i < container.length; i += 1) { | |
// Asign image source to variable (lazyload data-src !!) | |
const imageSource = container[i].querySelector('img').getAttribute('data-src'); | |
container[i].querySelector('img').style.display = 'none'; | |
container[i].style.backgroundSize = 'cover'; | |
container[i].style.backgroundImage = `url(' ${imageSource} ')`; | |
container[i].style.backgroundPosition = 'center center'; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment