Skip to content

Instantly share code, notes, and snippets.

@AndreKelling
Last active October 1, 2019 15:53
Show Gist options
  • Save AndreKelling/fbf8ba53c5492a8c51220516e4eb0043 to your computer and use it in GitHub Desktop.
Save AndreKelling/fbf8ba53c5492a8c51220516e4eb0043 to your computer and use it in GitHub Desktop.
CSS object-fit to background-image fallback // IE11
// 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