简体   繁体   English

“将” jQuery“转换为普通JavaScript”

[英]“Converting” jQuery to plain JavaScript

I was trying to make somehow similar page loader to the one in THIS codepan. 我试图在某种程度上使页面加载器与该代码加载器中的页面加载器类似。 What bothers me and was trying to solve on my own, was the use of what I guess are jQuery commands of some sort, the dollar signs ($). 让我感到困扰并试图自行解决的是使用某种我认为是jQuery命令的美元符号($)。 I was trying to write the code in plain JavaScript but the code won't work (as I guess I messed up something) Below are the JavaScript w/ jQuery and plain Javascript (that I made): 我试图用纯JavaScript编写代码,但是代码无法正常工作(因为我想弄乱了一些东西),下面是带jQuery的JavaScript和纯Javascript(我制作的):

Original code, with JavaScript and fragments of jQuery 原始代码,带有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> 

My code with plain JavaScript (not even close to complete I guess) 我的代码使用纯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> 

Any help would be much appreciated. 任何帮助将非常感激。

There are few issues in your code. 您的代码中几乎没有问题。

To use multiple selector, you can try querySelector() : 要使用多个选择器,可以尝试使用querySelector()

document.querySelector(".loader .image").innerHTML = ("" + images[counter] + "");

getElementsByClassName() returns collection, you have to use proper index: getElementsByClassName()返回collection,您必须使用适当的索引:

document.getElementsByClassName("spaner")[0].innerHTML= (num + '%');

Try the following: 请尝试以下操作:

 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> 

Unlike jQuery, that has the same API working on a single element as well as a jQuery collection of elements, the DOM API requires you to explicitly iterate over NodeList / HTMLCollection objects. 与jQuery不同,jQuery具有在单个元素和jQuery元素集合上使用的相同API,而DOM API则要求您显式遍历 NodeList / HTMLCollection对象。

Aside from that, instead of collecting all elements that have class loader and add to that list all elements that have class image you want to use querySelectorAll . 除此之外,除了收集具有类loader所有元素并将所有具有类image元素添加到该列表之外,您还需要使用querySelectorAll

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM