jQuery image preloader with animation & delay until loading finishes

by Ercan Cicek

HTML

<div class="loader"></div>
<div id="img-container"></div>

CSS

img {
  width: 50%;
  height: auto;
}

img {
  width: 50%;
  height: auto;
}

/* --- loading spinner - CREDIT: https://projects.lukehaas.me/css-loaders/ --- */
.loader {
  margin: 100px auto;
  font-size: 25px;
  width: 1em;
  height: 1em;
  border-radius: 50%;
  position: relative;
  text-indent: -9999em;
  -webkit-animation: load5 1.1s infinite ease;
  animation: load5 1.1s infinite ease;
  -webkit-transform: translateZ(0);
  -ms-transform: translateZ(0);
  transform: translateZ(0);
}
@-webkit-keyframes load5 {
  0%,
  100% {
    box-shadow: 0em -2.6em 0em 0em #ffffff, 1.8em -1.8em 0 0em rgba(0, 0, 0, 0.2), 2.5em 0em 0 0em rgba(0, 0, 0, 0.2), 1.75em 1.75em 0 0em rgba(0, 0, 0, 0.2), 0em 2.5em 0 0em rgba(0, 0, 0, 0.2), -1.8em 1.8em 0 0em rgba(0, 0, 0, 0.2), -2.6em 0em 0 0em rgba(0, 0, 0, 0.5), -1.8em -1.8em 0 0em rgba(0, 0, 0, 0.7);
  }
  12.5% {
    box-shadow: 0em -2.6em 0em 0em rgba(0, 0, 0, 0.7), 1.8em -1.8em 0 0em #ffffff, 2.5em 0em 0 0em rgba(0, 0, 0, 0.2), 1.75em 1.75em 0 0em rgba(0, 0, 0, 0.2), 0em 2.5em 0 0em rgba(0, 0, 0, 0.2), -1.8em 1.8em 0 0em rgba(0, 0, 0, 0.2), -2.6em 0em 0 0em rgba(0, 0, 0, 0.2), -1.8em -1.8em 0 0em rgba(0, 0, 0, 0.5);
  }
  25% {
    box-shadow: 0em -2.6em 0em 0em rgba(0, 0, 0, 0.5), 1.8em -1.8em 0 0em rgba(0, 0, 0, 0.7), 2.5em 0em 0 0em #ffffff, 1.75em 1.75em 0 0em rgba(0, 0, 0, 0.2), 0em 2.5em 0 0em rgba(0, 0, 0, 0.2), -1.8em 1.8em 0 0em rgba(0, 0, 0, 0.2), -2.6em 0em 0 0em rgba(0, 0, 0, 0.2), -1.8em -1.8em 0 0em rgba(0, 0, 0, 0.2);
  }
  37.5% {
    box-shadow: 0em -2.6em 0em 0em rgba(0, 0, 0, 0.2), 1.8em -1.8em 0 0em rgba(0, 0, 0, 0.5), 2.5em 0em 0 0em rgba(0, 0, 0, 0.7), 1.75em 1.75em 0 0em #ffffff, 0em 2.5em 0 0em rgba(0, 0, 0, 0.2), -1.8em 1.8em 0 0em rgba(0, 0, 0, 0.2), -2.6em 0em 0 0em rgba(0, 0, 0, 0.2), -1.8em -1.8em 0 0em rgba(0, 0, 0, 0.2);
  }
  50% {
    box-shadow: 0em -2.6em 0em 0em rgba(0, 0, 0, 0.2), 1.8em -1.8em 0 0em rgba(0, 0, 0, 0.2), 2.5em 0em 0 0em rgba(0, 0, 0, 0.5), 1.75em 1.75em 0 0em rgba(0, 0, 0,...

JavaScript

// --- resources needed
// --- jQuery recommended
(function() { 
  	var imgArr = [ "https://upload.wikimedia.org/wikipedia/commons/8/8c/Quillashapur_fort.jpg", "https://upload.wikimedia.org/wikipedia/commons/f/f7/Iglesia_de_San_Juan_el_Real%2C_Calatayud%2C_Espa%C3%B1a%2C_2017-01-08%2C_DD_25-27_HDR.jpg"
    ];
    preloadImages(imgArr);
})();

function preloadImages(imgArr) {
    var img,
        remaining = imgArr.length;
    for (var i = 0; i < imgArr.length; i++) {
      $("body").append("<img src='" + imgArr[i] + "' id='image-" + i + "' style='display: none;' />");
      img = document.getElementById("image-" + i);
      img.onload = function () {
        --remaining;
        if (remaining <= 0) {
          // --- all images loaded
          // --- example
          for(var i = 0; i < imgArr.length; i++) {
            $("#img-container").append("<img src='" + imgArr[i] + "' />");
            if(i === imgArr.length - 1) $(".loader").css("display", "none");
          }
          // --- /example
        }
      };
    }
}