将预加载器图标更改为图像

Change preloader icon to image

我正在使用来自 this website 的预加载程序代码。此代码会在几秒钟后旋转 fontawesome 图标并带有百分比。

基本上,我想显示图像而不是 fontawesome 图标。我试图改变但失败了。有没有办法把图标换成图片?

希望你们中的一些人能给我一些建议。谢谢!

$(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>

我已经用占位符图像替换了 JS 代码中的图标。随意使用您自己的图像。第一张图片在 HTML 代码中:

这里可以调整图片(在JS代码中):

在 CSS 代码中,我给出了 class“图像”position: relative;bottom: 50px;,以便图像看起来稍微高一点。您可以根据图像调整class

运行代码片段看:

$(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>

编码愉快!