I found a preloader code on this website that rotates fontawesome icons and displays the percentage after a few seconds.
However, I am looking to replace the fontawesome icon with an image but have been unsuccessful in my attempts. Is there a way to easily swap out the icon for an image?
I would appreciate any advice or suggestions on how to achieve this. 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>