Object-Fit Cover Internet Explorer Fix

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.

circle
oval portrait
oval landscape
square
landscape
portrait
resizable
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
// 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:
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;
  /* 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;*/
}

Author: , published: , last modified:

Kontakt

Udo Schmal

Udo Schmal
Softwareentwickler
Olvengraben 41
47608 Geldern
Nordrhein-Westfalen
Germany




+49 2831 9776557
+49 1575 0663676
+49 2831 1328709
SMS
WhatsApp

Google Maps Profile
Instagram Profile
vCard 3.0

Service Infos

CMS Info

Product Name:
UDOs Webserver
Version:
0.5.0.167
Description:
All in one Webserver
Copyright:
Udo Schmal
Compilation:
Sat, 25. Jun 2022 14:46:24

Development Info

Compiler:
Free Pascal FPC 3.3.1
compiled for:
OS:Linux, CPU:x86_64

System Info

OS:
Ubuntu 20.04.1 LTS (Focal Fossa)

Hardware Info

Model:
Hewlett-Packard HP Pavilion dm4 Notebook PC
CPU Name:
Intel(R) Core(TM) i5-2430M CPU @ 2.40GHz
CPU Type:
x86_64, 1 physical CPU(s), 2 Core(s), 4 logical CPU(s), 2681.460 MHz