繁体   English   中英

我可以在多个 Div 上使用相同的 JavaScript 函数吗?

[英]Can I use the same JavaScript Function on multiple Div's?

我是 javascript 的新手,我需要用动画条显示百分比的圆圈进度
幸运的是我已经做到了,但问题是我想用不同的百分比制作 5 个或更多相同的圆圈并且 **我需要从页面加载开始而不是通过单击 ** 那么如何在多个 div 上调用该函数使圆圈同时以不同的百分比出现

这是我的 JS 代码

 let firstLayer = document.querySelector('.cardHolder'), secondLayer = document.querySelector('.skillSecond'), startValue = 0, endValue = 50, duration = 20; let progress = setInterval(() => { startValue++ secondLayer.textContent = `${startValue}%` firstLayer.style.background = `conic-gradient(rgb(15, 176, 6) ${startValue * 3.6}deg, rgb(193, 193, 193) 0deg)` if (startValue == endValue) { clearInterval(progress) } }, duration);
 .cardHolder { display: flex; justify-content: center; align-items: center; background: conic-gradient(rgb(15, 176, 6) 3.6deg, rgb(193, 193, 193) 0deg); width: 100px; height: 100px; border-radius: 100%; }.skillFirst { display: flex; justify-content: center; align-items: center; background-color: #90697a; width: 80%; height: 80%; border-radius: 100%; }.skillSecond { font-family: 'Montserrat Subrayada', sans-serif; font-size: 24px; }
 <div class="cardHolder"> <div class="skillFirst"> <div class="skillSecond"> <span class="skillContent"></span> </div> </div> </div>

这就是我想出的。 基本上它的工作原理是制作一个包含每个单独进度条规范的对象数组。 然后遍历规范并开始每个间隔。

 let progression = [ { progressBarId: "first", progressBarLabelId: "first_label", startValue: 0, endValue: 50, duration: 20 }, { progressBarId: "second", progressBarLabelId: "second_label", startValue: 10, endValue: 100, duration: 10 }, { progressBarId: "third", progressBarLabelId: "third_label", startValue: 50, endValue: 80, duration: 20 } ]; window.onload = function() { for(var i = 0; i < progression.length; i++) { let firstLayer = document.getElementById(progression[i].progressBarId), secondLayer = document.getElementById(progression[i].progressBarLabelId), startValue = progression[i].startValue, endValue = progression[i].endValue, duration = progression[i].duration; let progress = setInterval(() => { startValue++ secondLayer.textContent = `${startValue}%` firstLayer.style.background = `conic-gradient(rgb(15, 176, 6) ${startValue * 3.6}deg, rgb(193, 193, 193) 0deg)` if (startValue == endValue) { clearInterval(progress) } }, duration); } }
 .cardHolder { display: flex; justify-content: center; align-items: center; background: conic-gradient(rgb(15, 176, 6) 3.6deg, rgb(193, 193, 193) 0deg); width: 100px; height: 100px; border-radius: 100%; }.skillFirst { display: flex; justify-content: center; align-items: center; background-color: #90697a; width: 80%; height: 80%; border-radius: 100%; }.skillSecond { font-family: 'Montserrat Subrayada', sans-serif; font-size: 24px; }
 <div class="cardHolder" id="first"> <div class="skillFirst"> <div class="skillSecond" id="first_label"> <span class="skillContent" ></span> </div> </div> </div> <div class="cardHolder" id="second"> <div class="skillFirst"> <div class="skillSecond" id="second_label"> <span class="skillContent" ></span> </div> </div> </div> <div class="cardHolder" id="third"> <div class="skillFirst"> <div class="skillSecond" id="third_label"> <span class="skillContent" ></span> </div> </div> </div>
希望这对您有所帮助,如果没有,请发表评论!

我怎样才能添加多个输入到这个<div>使用 javascript function?</div><div id="text_translate"><p> 对于上下文,这是我正在开发的基于 Django 的应用程序。 我在 HTML 文档中使用此脚本。 当按下按钮时,它会运行以下 function。 目的是创建一个元素,然后包含几个输入等等。 我遇到的问题是,当我使用以下 append 语句向 div 添加一个输入时,如果我尝试添加另一个输入类型(例如复选框),它会覆盖第一个输入(搜索)。 如何在脚本中进行 append 多个输入? 我总共需要 3 个不同的输入。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> var counter = 1; //limits amount of transactions function addElements() { if (counter &lt; 5) //only allows 4 additional transactions { let div = document.createElement('div'); div.id = 'row' + counter; document.body.appendChild(div); let input = document.createElement('input'); input.id='search'+counter; input.type = 'search'; input.placeholder = 'Search by product name' div.appendChild(input); let button = document.createElement('button'); button.id ='button'+counter; button.type = 'QRscan'; button.innerText = 'QR scan' div.appendChild(button); } counter++ if (counter &gt;= 6) { alert("You have reached the maximum transactions.") } } addElements();</pre></div></div><p></p></div>

[英]How can I add multiple inputs to this <div> using javascript function?

暂无
暂无

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

相关问题 如何使 function 适用于 javascript 中具有相同类的多个 div? 如何使用JavaScript使用相同的功能对多个输入字段进行数据验证? 我可以使用循环在元素ID递增的情况下多次执行相同的函数调用吗? 如何使用 javascript 变量作为 html 的 div id? 我怎样才能添加多个输入到这个<div>使用 javascript function?</div><div id="text_translate"><p> 对于上下文,这是我正在开发的基于 Django 的应用程序。 我在 HTML 文档中使用此脚本。 当按下按钮时,它会运行以下 function。 目的是创建一个元素,然后包含几个输入等等。 我遇到的问题是,当我使用以下 append 语句向 div 添加一个输入时,如果我尝试添加另一个输入类型(例如复选框),它会覆盖第一个输入(搜索)。 如何在脚本中进行 append 多个输入? 我总共需要 3 个不同的输入。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> var counter = 1; //limits amount of transactions function addElements() { if (counter &lt; 5) //only allows 4 additional transactions { let div = document.createElement('div'); div.id = 'row' + counter; document.body.appendChild(div); let input = document.createElement('input'); input.id='search'+counter; input.type = 'search'; input.placeholder = 'Search by product name' div.appendChild(input); let button = document.createElement('button'); button.id ='button'+counter; button.type = 'QRscan'; button.innerText = 'QR scan' div.appendChild(button); } counter++ if (counter &gt;= 6) { alert("You have reached the maximum transactions.") } } addElements();</pre></div></div><p></p></div> 如何在Twitter的Popover中使用“div”和多个按钮? 为什么不能在Javascript的同一函数范围内使用变量? 如何在Javascript中使用相同的passValue函数两次? 如何将相同的$(document).ready(function()用于多个输入? 多个ID调用同一个JavaScript函数
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM