如何解决将预加载器图标更改为图像
我正在使用来自 this website 的这个预加载器代码。这段代码在几秒钟后旋转了很棒的图标并带有百分比。
基本上,我想显示图像而不是 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 代码中,我给了类“图像”position: relative;
和 bottom: 50px;
,以便图像看起来更高一点。您可以根据图像调整类
运行代码片段以查看:
$(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">',];
$(".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>
编码快乐!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。