Pure CSS spinner in reality

... with CSS & javascript ...

Ship deck ventilation pipe, left side

Scale Model 1

Ship deck ventilation pipe, right side

Scale Model 2

Special picture !

CSS fun

Small round park

Former Pond

Landscape with lake

Calm Afternoon

Realisation

New time table:

Construction

<!DOCTYPE html>
<html lang="...">
<head>
  ...
  <link rel="stylesheet" href="technix/spinner-styles.css">
  <script>
    function show404(n) {
      n.parentNode.className
      = n.parentNode.className + " missingImg";
    }
  </script>

</head>

<body>
  ...
  <div class="imgBox">
    <div class="spinner"></div>
    <img src="images/..." alt="..." onerror="show404(this)">
  </div>
  ...
  <div class="imgBox">
    <div class="spinner"></div>
    <img src="images/..." alt="..." onerror="show404(this)">
  </div>
  ...
  <script src="technix/spinner-script.js"></script>
</body>
</html>
/* spinner-script.js */

document.body.className
  = document.body.className
  + " javascripted";
setTimeout(imgCompleteCheck,100);

function imgCompleteCheck(){
  var allImgs
  = document.getElementsByTagName("img");

  for (var i=0; i<allImgs.length; i++){
    if(allImgs[i].parentNode.className
    === "imgBox"){
      let thisImg=allImgs[i];
      thisImg.style.opacity=0;
      thisImg.style.animation="none";
      let thisSpinnerTimer=0;
      checkThisImg();

      function checkThisImg(){
        let tmpImg = new Image();
        tmpImg.src = thisImg.src;
        let timeOutThisCheck;

        if(tmpImg.complete == false){
          if (thisSpinnerTimer == 1100){
            // slow loading
            thisImg.parentNode.className
            = "imgBox spinnerStart";
          }
          thisSpinnerTimer
          = thisSpinnerTimer+50;
          timeOutThisCheck
          = setTimeout(checkThisImg, 50);
        }
        else {
          if (thisSpinnerTimer < 1100){
            // fast loaded
            thisImg.style.animation
            = ".5s .5s spinnerFadeIn
               ease-in forwards";
          }
          else { // slow loaded
            thisImg.parentNode.className
            = "imgBox loadingFinished";
            thisImg.style.animation
            = "1.5s .5s spinnerFadeIn
               ease-in forwards";
          }
          tmpImg.src = "";
          clearTimeout(timeOutThisCheck);
        }
      }
    }
  }
}
.imgBox  { /* styles as before */ } .spinner { /* styles as before */ }
/* resetting styles if javascript is enabled: */
.javascripted .imgBox:before {
content: "... Image is loading ...";
left: 50%;
margin-left: -4.7em;
right: auto;
color: green;
}

.javascripted .imgBox:before,
.javascripted .imgBox:after,
.javascripted .spinner {

display: none; animation: none;
}

/* extra classes if javascript is enabled:
.imgBox.missingImg:after {
display: block;
content: "Oops, this image is missing ...";
animation: 2s spinnerFadeIn ease-in-out forwards;
}

.imgBox.spinnerStart:before {
display: block;
animation: 1s 1s spinnerFadeIn ease-in-out forwards;
}

.imgBox.loadingFinished:before {
display: block;
animation: .8s spinnerFadeOut ease-in forwards;
}

.imgBox.loadingFinished .spinner {
display: block;
animation: 1.5s spinning linear 10,
  .8s spinnerFadeOut ease-in forwards;
}

Files

The basic styles of the imgBox are in the separate spinner-styles.css and spinner-script.js

What to do