我正在尝试制作某种类似于THIS代码库中的页面加载器的页面加载器。 让我感到困扰并试图自行解决的是使用某种我认为是jQuery命令的美元符号($)。 我试图用纯JavaScript编写代码,但是代码无法正常工作(因为我想弄乱了某些东西) 以下是带有jQuery的JavaScript和纯Javascript(我制作的):
带有JavaScript和jQuery片段的原始代码
$(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;
-webkit-transform-origin: bottom center;
-moz-transform-origin: bottom center;
-ms-transform-origin: bottom center;
animation: 3s rotate infinite;
-webkit-animation: 3s rotate infinite;
-moz-animation: 3s rotate infinite;
-ms-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);
-webkit-transform: rotate(90deg);
-moz-transform: rotate(90deg);
-ms-transform: rotate(90deg);
}
10% {
opacity: 0;
}
35% {
transform: rotate(0deg);
-webkit-transform: rotate(0deg);
-moz-transform: rotate(0deg);
-ms-transform: rotate(0deg);
opacity: 1;
}
65% {
transform: rotate(0deg);
-webkit-transform: rotate(0deg);
-moz-transform: rotate(0deg);
-ms-transform: rotate(0deg);
opacity: 1;
}
80% {
opacity: 0;
}
100% {
transform: rotate(-90deg);
-webkit-transform: rotate(-90deg);
-moz-transform: rotate(-90deg);
-ms-transform: rotate(-90deg);
}
}
<link href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="loader">
<div class="image">
<i class="fa fa-codepen"></i>
</div>
<span></span>
</div>
我的代码使用普通的JavaScript(我想还不太完整)
document.addEventListener("DOMContentLoaded", function() {
var counter = 0;
// Start the changing images
setInterval(function() {
if(counter == 4) {
counter = 0;
}
changeImage(counter);
counter++;
}, 3000);
// Set the percentage off
loading();
});
function changeImage(counter) {
var images = [
'<i class="fa fa-cubes"></i>',
'<i class="fa fa-rocket"></i>',
'<i class="fa fa-pie-chart"></i>',
'<i class="fa fa-codepen"></i>'
];
document.getElementsByClassName("loader", "image").innerHTML = ("" + images[counter] + "");
}
function loading(){
var num = 0;
for(i=0; i<=100; i++) {
setTimeout(function() {
document.getElementsByClassName("spaner").innerHTML= (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;
-webkit-transform-origin: bottom center;
-moz-transform-origin: bottom center;
-ms-transform-origin: bottom center;
animation: 3s rotate infinite;
-webkit-animation: 3s rotate infinite;
-moz-animation: 3s rotate infinite;
-ms-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);
-webkit-transform: rotate(90deg);
-moz-transform: rotate(90deg);
-ms-transform: rotate(90deg);
}
10% {
opacity: 0;
}
35% {
transform: rotate(0deg);
-webkit-transform: rotate(0deg);
-moz-transform: rotate(0deg);
-ms-transform: rotate(0deg);
opacity: 1;
}
65% {
transform: rotate(0deg);
-webkit-transform: rotate(0deg);
-moz-transform: rotate(0deg);
-ms-transform: rotate(0deg);
opacity: 1;
}
80% {
opacity: 0;
}
100% {
transform: rotate(-90deg);
-webkit-transform: rotate(-90deg);
-moz-transform: rotate(-90deg);
-ms-transform: rotate(-90deg);
}
}
<link href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="loader">
<div class="image">
<i class="fa fa-codepen"></i>
</div>
<span class="spaner"></span>
</div>
任何帮助将不胜感激。
答案 0 :(得分:4)
您的代码中几乎没有问题。
要使用多个选择器,可以尝试querySelector()
:
document.querySelector(".loader .image").innerHTML = ("" + images[counter] + "");
getElementsByClassName()
返回集合,您必须使用正确的索引:
document.getElementsByClassName("spaner")[0].innerHTML= (num + '%');
尝试以下操作:
document.addEventListener("DOMContentLoaded", function() {
var counter = 0;
// Start the changing images
setInterval(function() {
if(counter == 4) {
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>'
];
document.querySelector(".loader .image").innerHTML = ("" + images[counter] + "");
}
function loading(){
var num = 0;
for(i=0; i<=100; i++) {
setTimeout(function() {
document.getElementsByClassName("spaner")[0].innerHTML= (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://use.fontawesome.com/releases/v5.7.1/css/all.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="loader">
<div class="image">
<i class="fa fa-codepen"></i>
</div>
<span class="spaner"></span>
</div>
答案 1 :(得分:2)
与jQuery不同,jQuery具有相同的API可以处理单个元素以及jQuery元素集合,而DOM API则要求您在NodeList
/ {{3 }}对象。
除此之外,您不想使用所有loader
类的元素并将所有image
类的元素添加到列表中,而是要使用HTMLCollection
。
document.addEventListener("DOMContentLoaded", function() {
var counter = 0;
// Start the changing images
setInterval(function() {
if(counter == 4) {
counter = 0;
}
changeImage(counter);
counter++;
}, 3000);
// Set the percentage off
loading();
});
function changeImage(counter) {
var images = [
'<i class="fa fa-cubes"></i>',
'<i class="fa fa-rocket"></i>',
'<i class="fa fa-pie-chart"></i>',
'<i class="fa fa-codepen"></i>'
];
[...document.querySelectorAll(".loader .image")].forEach((loader) => {
loader.innerHTML = images[counter];
})
}
function loading(){
var num = 0;
for(i=0; i<=100; i++) {
setTimeout(function() {
[...document.getElementsByClassName("spaner")].forEach((spaner) => {
spaner.innerHTML = 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://use.fontawesome.com/releases/v5.7.1/css/all.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="loader">
<div class="image">
<i class="fa fa-codepen"></i>
</div>
<span class="spaner"></span>
</div>