简体   繁体   English

如何使用 JavaScript 为简单的进度条设置动画?

[英]How to animate a simple progress bar using JavaScript?

Here is a loading bar script in CSS... I want to animate it using a Javascript function... But I have no idea where to start?这是 CSS 中的加载条脚本...我想使用 Javascript function 对其进行动画处理...但我不知道从哪里开始?

Here is the code:这是代码:

I have tried this CSS code to animate it but using CSS:我试过这个 CSS 代码来动画它,但使用 CSS:

 * { margin: 0px; padding: 0px; border: 0px; font-family: sans-serif; cursor: default; -webkit-user-select: none; -ms-user-select: none; -o-user-select: none; user-select: none; } body { background: #ffff; } #pb { background: #transparent; width: 250px; height: 38px; position: absolute; top: 50%; left: 50%; margin-top: -19px; margin-left: -125px; border-radius: 8px; padding: 3px; overflow: hidden; text-align: center; } #progress { height: 38px; width: 250px; background-color: #0080FF; border-radius: 6px; background: #6db3f2; background: -moz-linear-gradient(top, #6db3f2 0%, #54a3ee 50%, #3690f0 51%, #1e69de 100%); /* FF3.6+ */ background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #6db3f2), color-stop(50%, #54a3ee), color-stop(51%, #3690f0), color-stop(100%, #1e69de)); /* Chrome,Safari4+ */ } #done:active { -webkit-box-shadow: inset 0px 0px 18px rgba(0, 0, 0, 0.4); box-shadow: inset 0px 0px 18px rgba(0, 0, 0, 0.4); border-radius: 6px; } #done { text-align: center; line-height: 38px; font-size: 14px; font-weight: bold; color: #EEE; height: 38px; width: 250px; text-shadow: 0px -1px 0px rgba(0, 0, 0, 0.2); cursor: pointer; } #pb:hover #progress { transition: all 10.3s; width: 10px; }
 <div id="pb"> <div id="progress"> <div id="done">Loading...</div> </div> Done! </div>

But I want a Javascript function to animate the progress bar in 10 seconds.但我想要一个 Javascript function 在 10 秒内为进度条设置动画。

Any help would be greatly appreciated任何帮助将不胜感激

You can simply do this:你可以简单地这样做:

let elem = document.getElementById("done");   
let width = 1;
let id = setInterval(frame, 100);
function frame() {
    if (width >= 100) {
        clearInterval(id);
} else {
    width++; 
    elem.style.width = width + '%'; 
}

Also there is ProgressBar.js.还有 ProgressBar.js。 You can check out this site for more modern and good looking progress bars.您可以查看此站点以获取更现代、更美观的进度条。 It allows you to look the codes in JSFiddle and it is easy to use.它允许您查看 JSFiddle 中的代码,并且易于使用。

You can trigger the animation of this function with javascript.您可以使用 javascript 触发此 function 的 animation。 You can even set its transition duration!您甚至可以设置其过渡持续时间!

 document.querySelector("button").addEventListener("click", () => { document.querySelector(".progress.bar").style.transitionDuration = "10s"; document.querySelector(".progress").className += " complete"; });
 .progress { width: 50%; height: 2em; border: 1px solid black; }.bar { width: 100%; background-color: deepskyblue; color: white; text-align: center; line-height: 2em; transition-property: width; transition-timing-function: linear; }.progress.complete.bar { width: 0%; } button { margin-top: 1em; }
 <div class="progress"> <div class="bar">Loading...</div> </div> <button> Start </button>

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

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