Home >Web Front-end >JS Tutorial >Simple and practical progress bar loading component loader.js

Simple and practical progress bar loading component loader.js

WJ
WJforward
2020-06-01 17:02:152656browse

Simple and practical progress bar loading component loader.js

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:

Simple and practical progress bar loading component loader.js

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 = $(&#39;#loader&#39;).find(&#39;.progress-bar&#39;);    var $value = $bar.find(&#39;.progress-value&#39;);    var Task = function (index, duration) {
        setTimeout(function () {
            var p = (index / 4 * 100).toFixed(0) + &#39;%&#39;;
            $bar.css(&#39;width&#39;,p);
            $value.text(p);
            console.log(&#39;第&#39; + index + &#39;个异步任务执行完毕&#39;);
        }, 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:

Simple and practical progress bar loading component loader.js

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:

Simple and practical progress bar loading component loader.js

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:

Simple and practical progress bar loading component loader.js

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!

Statement:
This article is reproduced at:51dev.com. If there is any infringement, please contact admin@php.cn delete