This article provides a simple method to implement the progress bar loading effect of a process, so that it can be used to better feedback the completion progress of time-consuming tasks on the page. To implement this function, you must first consider how to implement a static progress bar effect, similar to the following:
This is relatively simple, just two divs, bootstrap official It provides progress bar components with multiple themes. If you want to use it yourself, just refer to other people's code and write it in your own style. It is actually very easy to understand:
.progress { height: 20px; background-color: #f5f5f5; border-radius: 4px; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);}.progress-bar { float: left; width: 0; height: 100%; font-size: 12px; line-height: 20px; color: #fff; text-align: center; background-color: #337ab7; box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15); position: relative; border-radius: 4px;}
The second step is to consider how to calculate the progress. Take resource loading as an example. If it is a client, we usually have permission to read the actual size of the resource, so when calculating the loading progress, we only need to divide the amount of data that has been loaded by the total amount of data to be loaded. ; But on the web page, we do not have the ability to get the size of the resources to be loaded, so we can only use a less accurate solution, dividing the number of loaded resources by the total number of resources. Based on the following calculation method, we only need to calculate the completed task progress at the moment each time-consuming task is completed, and then set the corresponding width of the progress bar.
Below I use a timer to simulate 4 asynchronous tasks that are initiated at the same time but require different times to complete to implement the function of this step:
<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <link href="loader.css" rel="stylesheet"></head><body><div id="loader" class="loader"> <div class="progress"> <div class="progress-bar progress-bar-striped"> <div class="progress-value"></div> </div> </div></div></body><script src="jquery.js"></script><script> var $bar = $('#loader').find('.progress-bar'); var $value = $bar.find('.progress-value'); var Task = function (index, duration) { setTimeout(function () { var p = (index / 4 * 100).toFixed(0) + '%'; $bar.css('width',p); $value.text(p); console.log('第' + index + '个异步任务执行完毕'); }, duration); }; //模拟四个同时发起的异步任务 var task1 = new Task(1, 1000); var task2 = new Task(2, 3000); var task3 = new Task(3, 5000); var task4 = new Task(4, 7000);</script></html>
The actual effect is as follows:
When we reach this step, we have actually implemented a basic progress bar loading function. However, the above effect does not seem to be a very good experience. It would be great if the progress values of this progress bar could be changed continuously, like the following:
For To achieve this step, some people may think of using transition. By setting a transition similar to width .2s for the progress bar, then when the width of the progress bar changes, you can naturally see the effect of the progress bar changing continuously. There are two problems with this approach:
1. The number cannot change continuously, because the number cannot be transitioned from one value to another through transition;
2. The progress bar cannot be seen Load to 100%, because when the time-consuming task completion progress is 100%, in addition to setting the width of the progress bar to 100%, there is usually logic to hide or remove the progress bar, and the progress bar has a transition It takes a certain amount of time to transition from its original width to 100%, so users cannot see 100%.
However, these two are not big problems. Progress bars without progress numbers are also very common; the effect of the progress bar entering the main function scene when it is less than 100% is also very common, and this effect can sometimes give Users have the illusion that it loads really quickly. .
If you want to struggle with the above two problems, how to implement a function that has numbers and progress that can satisfy continuous changes, and only enters the main scene after the progress bar has 100% displayed the loading effect? Just like the following similar effect:
In this requirement, I think there are two points that need to be paid attention to:
First, when a task is completed At this time, the remaining tasks may not be completed yet. At this time, the progress bar will enter the waiting state. You have to wait until other tasks are completed and there is new progress before you can see the next loading effect;
二It is the callback control when the progress bar is loaded to 100%. When the task completion progress is 100%, the progress bar may not be 100%. It will take time for the progress bar to change from its current value to 100%, so It turns out that some logic added when the task completion progress is 100%, such as entering the main scene, must be processed at the moment when the progress bar is loaded to 100%.
Based on the above, my idea is:
1. Divide the changes in the progress bar into multiple segments, because each completion of a time-consuming task will correspond to a progress value, and these values are greater than 0 and Less than or equal to 100, taking four time-consuming tasks as an example, they will divide the progress bar into three segments: 0-25, 25-50, 75-100;
2. Abstract the segmentation of step 1 It becomes a loading task with a progress bar. This task has two basic attributes: loading time and change interval. Make this task an animation. During each execution of the animation, provide a callback to the outside and pass in the current progress value to set the width of the progress bar. The current progress value can be calculated based on the time the animation has been executed, the loading time and the change interval. The change interval corresponds to the percentage range in step 1. The loading time can be calculated by changing the interval range * the time required for the progress bar to load 1%. In other words, the time required to load 1% of the animation should be regarded as a constant. For more convenience, the time required to load the animation from 0 to 100% is used as a constant for better control.
3. Define a queue to store the abstract loading tasks in step 2. Control the execution timing of the first task in the queue; every time a task is executed, the next one is automatically executed.
4. When the task progress is 100% and the last task in the queue is completed, notify the outside for a callback.
The actual effect of this demo is exactly the same as the previous gif.
So far, we have got a component for controlling the loading effect of the progress bar that looks relatively practical. However, it is not without its problems. The problem is that the time it takes for the progress bar to load will definitely be greater than the time it takes for the progress bar we set in step 2 to load from 0 to 100% at one time. In other words, this approach will deliberately delay the entire process of a time-consuming task. Therefore, in actual use, the constant mentioned above cannot be defined too long.
Finally, this component can be used in conjunction with a component I wrote before about image preloading to create a more perfect image preloading effect. If you are interested, you can try it.
I hope the content of this article will be helpful to everyone’s practical work.
The above is the detailed content of Simple and practical progress bar loading component loader.js. For more information, please follow other related articles on the PHP Chinese website!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Atom editor mac version download
The most popular open source editor
