Search code examples
htmljquerycssfont-awesomepreloader

Change preloader icon to image


I'm using this preloader code from this website. This code rotating fontawesome icons after a few second and come with the percentage.

Basically, I want to display image instead of fontawesome icon. I tried to change but failed. Is that any way to change replace the icon to image?

Hoping that some of you could provide me with some advice. Thank you!

$(document).ready(function() {
  var counter = 0;

  // Start the changing images
  setInterval(function() {
    if(counter == 9) { 
      counter = 0; 
    }

    changeImage(counter);
    counter++;
  }, 3000);

  // Set the percentage off
  loading();
});

function changeImage(counter) {
  var images = [
    '<i class="fa fa-fighter-jet"></i>',
    '<i class="fa fa-gamepad"></i>',
    '<i class="fa fa-headphones"></i>',
    '<i class="fa fa-cubes"></i>',
    '<i class="fa fa-paw"></i>',
    '<i class="fa fa-rocket"></i>',
    '<i class="fa fa-ticket"></i>',
    '<i class="fa fa-pie-chart"></i>',
    '<i class="fa fa-codepen"></i>'
  ];

  $(".loader .image").html(""+ images[counter] +"");
}

function loading(){
  var num = 0;

  for(i=0; i<=100; i++) {
    setTimeout(function() { 
      $('.loader span').html(num+'%');

      if(num == 100) {
        loading();
      }
      num++;
    },i*120);
  };

}
@import url(https://fonts.googleapis.com/css?family=Lato:100,300,400,700,900);

html,body {
  margin: 0;
  padding: 0;
  font-family:'Lato', sans-serif;
}
.loader {
  width: 100px;
  height: 80px;
  position: absolute;
  top: 0; right: 0; left: 0; bottom: 0;
  margin: auto;
}
.loader .image {
  width: 100px;
  height: 160px;
  font-size: 40px;
  text-align: center;
  transform-origin: bottom center;
  animation: 3s rotate infinite;
  opacity: 0;
}
.loader span {
  display: block;
  width: 100%;
  text-align: center;
  position: absolute;
  bottom: 0;
}

@keyframes rotate{
  0% {
    transform: rotate(90deg);
  }
  10% {
    opacity: 0;
  }
  35% {
    transform: rotate(0deg);
    opacity: 1;
  }
  65% {
    transform: rotate(0deg);
    opacity: 1;
  }
  80% {
    opacity: 0;
  }
  100% {
    transform: rotate(-90deg);
  }
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<div class="loader">
  <div class="image">
    <i class="fa fa-codepen"></i>
  </div>
  <span></span>
</div>


Solution

  • I have replaced the icons in the JS code with placeholder Images. Feel free to use your own images. The very first image is here in the HTML code:

    First Icon (Picture)

    Here you can adjust the images (In the JS Code):

    Adjust your own images

    In the CSS code I have given the class "image" position: relative; and bottom: 50px; so that the image appears a little higher up. You can adjust the class depending on the images

    Run code snippet to see:

    $(document).ready(function() {
      var counter = 0;
    
      // Start the changing images
      setInterval(function() {
        if(counter == 9) { 
          counter = 0; 
        }
    
        changeImage(counter);
        counter++;
      }, 3000);
    
      // Set the percentage off
      loading();
    });
    
    function changeImage(counter) {
      var images = [
        '<img src="http://via.placeholder.com/100">',
        '<img src="http://via.placeholder.com/100">',
        '<img src="http://via.placeholder.com/100">',
        '<img src="http://via.placeholder.com/100">',
        '<img src="http://via.placeholder.com/100">',
        '<img src="http://via.placeholder.com/100">',
        '<img src="http://via.placeholder.com/100">',
        '<img src="http://via.placeholder.com/100">',
        '<img src="http://via.placeholder.com/100">',
        '<img src="http://via.placeholder.com/100">',
      ];
    
      $(".loader .image").html(""+ images[counter] +"");
    }
    
    function loading(){
      var num = 0;
    
      for(i=0; i<=100; i++) {
        setTimeout(function() { 
          $('.loader span').html(num+'%');
    
          if(num == 100) {
            loading();
          }
          num++;
        },i*120);
      };
    
    }
    @import url(https://fonts.googleapis.com/css?family=Lato:100,300,400,700,900);
    
    html,body {
      margin: 0;
      padding: 0;
      font-family:'Lato', sans-serif;
    }
    .loader {
      width: 100px;
      height: 80px;
      position: absolute;
      top: 0; right: 0; left: 0; bottom: 0;
      margin: auto;
    }
    .loader .image {
      width: 100px;
      height: 160px;
      font-size: 40px;
      text-align: center;
      transform-origin: bottom center;
      animation: 3s rotate infinite;
      opacity: 0;
    }
    .loader span {
      display: block;
      width: 100%;
      text-align: center;
      position: absolute;
      bottom: 0;
    }
    
    .image {
      position: relative;
      bottom: 50px;
    }
    
    @keyframes rotate{
      0% {
        transform: rotate(90deg);
      }
      10% {
        opacity: 0;
      }
      35% {
        transform: rotate(0deg);
        opacity: 1;
      }
      65% {
        transform: rotate(0deg);
        opacity: 1;
      }
      80% {
        opacity: 0;
      }
      100% {
        transform: rotate(-90deg);
      }
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <div class="loader">
      <div class="image">
        <img src="http://via.placeholder.com/100">
      </div>
      <span></span>
    </div>

    HAPPY CODING!