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:
object-fit.htm HTML (780 Bytes) 30.11.2021 23:54
<!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:
object-fit.js JavaScript (1,98 kByte) 16.10.2021 23:02
(function () {
'use strict' ;
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();
}
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]);
}
}
}
}
})();
StyleSheet:
object-fit.css StyleSheet (1,07 kByte) 21.04.2021 15:37
div .circle , div .oval , div .oval2 , div .square , div .landscape , div .portrait , div .resizable {
position : relative ;
display : inline-block ;
border : 1px solid #000 ;
position : relative ;
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 : 45% ;
}
img .fit-cover {
position : absolute ;
top : 50% ;
left : 50% ;
transform : translate(-50%, -50% ) ;
width : 100% ;
height : 100% ;
}