gpt4 book ai didi

javascript - "Converting"jQuery 到纯 JavaScript

转载 作者:行者123 更新时间:2023-11-28 04:44:11 25 4
gpt4 key购买 nike

我试图制作与 THIS 中的类似的页面加载器密码盘。困扰我并试图自己解决的是我猜是某种 jQuery 命令的使用,即美元符号 ($)。我试图用纯 JavaScript 编写代码,但代码不起作用(因为我想我搞砸了一些东西)下面是带有 jQ​​uery 的 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>

如有任何帮助,我们将不胜感激。

最佳答案

您的代码中几乎没有问题。

要使用多个选择器,你可以试试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>

关于javascript - "Converting"jQuery 到纯 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54627818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com