The progress bar cannot be used to obtain the size of the entire page and the size of the current page. Therefore, you can only use the progress bar for simulation. Next, we will introduce the implementation method, if you are interested, you can refer to the following. Because we cannot obtain the size of the entire page and the current loading amount in any way, the only way to create a loading progress bar is to simulate it. How can we simulate it?
We know that the page is executed from top to bottom, that is to say, we can roughly estimate how much is loaded at a location on the page, and then use jq to simulate a progress bar for display.
First, let's draw a progress bar, that is, the figure. You don't need to describe it too much and read the code yourself.
CSS
The Code is as follows:
. Loading {position: relative; top: 0; left: 0}
. Tip1 {float: left; background: # A70000; color: # fff; height: 32px; line-height: 32px; padding: 0 15px; border: 0; position: relative}
. Jindu {float: left; margin-left: 20px; color: # fff; width: 150px; height: 32px; line-height: 32px; background: #000; position: relative}
. Jindu B {color: # A70000; width: 0; height: 0; font-size: 0px; border-width: 10px; border-color: # fff # A70000; border-style: solid; position: absolute; left:-20px; top: 5px; overflow: hidden}
. Jindu. jindu2 {width: 0px; height: 32px; line-height: 32px; background: # A70000; position: absolute}
. Jindu. text {width: 150px; height: 32px; line-height: 32px; text-align: center; position: absolute}
HTML
The Code is as follows:
System initializing
Total page progress 0%
At this time, we should reference the jquery library. The reference is not in the head area, but written below the html code. The reason why we put the style in the head is to ensure that the style is loaded in the first step of page loading, so that the page will not be messy. JS does not need it. In addition, a large file on the page is mainly js, so loading js in the body is for the sake of progress bar.
The progress bar has been drawn, and jquery has referenced it. Now we need to write a method to make the progress bar move.
The Code is as follows:
Var loading = function (a, B ){
Var c = B * 1.5;
If (B = 100 ){
$ ('. Bgloader. jindu2'). animate ({width: c + 'px'}, 500, function (){
$ ('. Bgloader. tip1'). text ();
$ ('. Bgloader font'). text (B );
$ ('. Bgloader. loading'). animate ({top: '-32px'}, 1000, function (){
$ ('. Bgloader'). fadeOut ();
});
});
} Else {
$ ('. Bgloader. jindu2'). animate ({width: c + 'px'}, 500, function (){
$ ('. Bgloader. tip1'). text ();
$ ('. Bgloader font'). text (B );
});
}
};
Here I wrote a loading (a, B). The two parameters are the loading content prompt information and the loading progress percentage respectively. Then I used several other js libraries to test the loading progress.
The Code is as follows: