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.

HTML:
object-fit.htm HTML (774 bytes) 28.01.2021 23:50
<!DOCTYPE html >
<html lang="de">
  <head>
    <meta charset="utf-8" />
    <title>Object-fit: cover</title>
    <link rel="stylesheet" href="object-fit.css" />
  </head>
  <body>
    <div class="circle"><img class="fit-cover" src="image~200.jpg" /></div>
    <div class="oval"><img class="fit-cover" src="image~200.jpg" /></div>
    <div class="oval2"><img class="fit-cover" src="image~400.jpg" /></div>
    <div class="square"><img class="fit-cover" src="image~200.jpg" /></div>
    <div class="landscape"><img class="fit-cover" src="image~200.jpg" /></div>
    <div class="portrait"><img class="fit-cover" src="image~200.jpg" /></div>
    <div class="resizable"><img class="fit-cover" src="image~800.jpg" /></div>
    <script src="object-fit.js"></script>
  </body>
</html>
JavaScript:
object-fit.js JavaScript (1,55 kByte) 08.02.2021 17:27
// coding: utf-8
/** Created by: Udo Schmal | https://www.gocher.me/ */
(function() {
  'use strict';
  /*if ('objectFit' in document.documentElement.style === false) {*/
    console.log('object-fit fix');
    function cover(el) {
      var bounding = el.parentNode.getBoundingClientRect(), 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;
        }
      }
      console.log('use object-fit.js for ' + src);
      if ((bounding.width / bounding.height) > (width / height)) {
        el.style.height = 'auto';
      } else {
        el.style.width = 'auto';
      }
    }
    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 (989 bytes) 08.02.2021 18:48
div {
  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

Instagram Profile
vCard 3.0

Service Infos

CMS Info Product Name:
UDOs Webserver
Version:
0.5.0.23
Description:
All in one Webserver
Copyright:
Udo Schmal
Compilation:
Sat, 27. Feb 2021 23:59:19
Development Info Compiler:
Free Pascal FPC 3.3.1
compiled for:
OS:Linux, CPU:x86_64
System Info OS:
Ubuntu 20.04.2 LTS (Focal Fossa)
Hardware Info Model:
Hewlett-Packard HP Pavilion dv7 Notebook PC
CPU Name:
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
CPU Type:
x86_64, 1 physical CPU(s), 2 Core(s), 4 logical CPU(s), 2930.883 MHz