Im Beispiel habe ich komplett auf die CSS Variante object-fit:cover
verzichtet, damit man die Funktion auch in anderen Browsern überprüfen kann.
Möchte man diese Variante nur im Internet Explorer als Fallback nutzen sind lediglich die Auskommentierung im JavaScript und im StyleSheet zu entfernen.
HTML:
<!DOCTYPE html >
<html lang="de">
<head>
<meta charset="utf-8" />
<title>Object-fit: cover</title>
</head>
<body>
<div class="circle"><img class="fit-cover" src="/media/gocher~200.JPG" /></div>
<div class="oval"><img class="fit-cover" src="/media/gocher~200.JPG" /></div>
<div class="oval2"><img class="fit-cover" src="/media/gocher~400.JPG" /></div>
<div class="square"><img class="fit-cover" src="/media/gocher~200.JPG" /></div>
<div class="landscape"><img class="fit-cover" src="/media/gocher~200.JPG" /></div>
<div class="portrait"><img class="fit-cover" src="/media/gocher~200.JPG" /></div>
<div class="resizable"><img class="fit-cover" src="/media/gocher~800.JPG" /></div>
<script src="object-fit.js"></script>
</body>
</html>
JavaScript:
// coding: utf-8
/** Created by: Udo Schmal | https://www.gocher.me/ */
(function() {
'use strict';
// load stylesheet
if (!document.getElementById('object-fit.css')) {
var style = document.createElement("link");
style.type = 'text/css';
style.href = '/code/object-fit/object-fit.css';
style.id = 'object-fit.css';
style.rel = 'stylesheet';
document.head.appendChild(style);
style.addEventListener('load', fit);
} else {
fit();
}
/*if ('objectFit' in document.documentElement.style === false) {*/
console.log('object-fit fix');
function cover(el) {
var width, height, src;
if (el.tagName.toLowerCase() == 'img') {
width = el.naturalWidth;
height = el.naturalHeight;
src = el.src;
} else if (el.tagName.toLowerCase() == 'video') {
width = el.videoWidth;
height = el.videoHeight;
var els = el.getElementByTagName('source');
if (els && els.length > 0) {
src = els[0].src;
}
}
let bounding = el.parentNode.getBoundingClientRect();
console.log('use object-fit.js for ' + src);
if ((bounding.width / bounding.height) > (width / height)) {
el.style.height = 'auto';
} else {
el.style.width = 'auto';
}
}
function fit() {
var els = document.querySelectorAll('.fit-cover'), len = els.length, i;
for (i=0; i < len; i++) {
if (els[i].tagName.toLowerCase() == 'img') {
if (!els[i].complete || (els[i].natuaralWidth === 0)) {
els[i].addEventListener("load", function (event) { cover(this); });
} else {
cover(els[i]);
}
} else if (!els[i].tagName.toLowerCase() == 'video') {
if (els[i].videoWidth == 0) {
els[i].addEventListener("loadedmetadata", function (event) { cover(this); });
} else {
cover(els[i]);
}
}
}
}
/*} else {
console.log('native object-fit');
}*/
})();
StyleSheet:
div.circle, div.oval, div.oval2, div.square, div.landscape, div.portrait, div.resizable {
position: relative;
display: inline-block;
border: 1px solid #000;
/* img/video align to wrapper div */
position: relative;
/* hide overflow of img/video */
overflow: hidden;
}
.circle {
border-radius: 50% 50%;
width: 100px;
height: 100px;
}
.oval {
border-radius: 100px / 50px;
width: 200px;
height: 100px;
}
.oval2 {
border-radius: 50px / 100px;
width: 100px;
height: 200px;
}
.square {
width: 100px;
height: 100px;
}
.landscape {
width: 150px;
height: 100px;
}
.portrait {
width: 100px;
height: 150px;
}
.resizable {
width: 100%;
height: 0;
min-height: 1px;
/*padding-top: 56.25%;*/
padding-top: 45%;
}
img.fit-cover{
/* img/video align to wrapper div */
position: absolute;
/* center image/video in wrapper div */
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
/* resize to wrapper (skew) */
width: 100%;
height: 100%;
/* depending on the aspect ratio, the protruding part is set to auto */
/*object-fit: cover;*/
}