Simple implementation of JS Loading Functions

Source: Internet
Author: User

We often see the LOADING prompt when LOADING data when browsing the Web page. In fact, the principle of this function is very simple, that is, a DIV is used to cover the current page, and then Loading is displayed on the covering DIV layer. Now let's implement it.

1. Current page:
Copy codeThe Code is as follows:
<Div class = "current"> <a href = "#" onclick = "showLoading ()"> Loading </a> </div>

2. Mask Layer:
Copy codeThe Code is as follows:
<Div id = "over" class = "over"> </div>

3. Loading presentation layer:
Copy codeThe Code is as follows:
<Div id = "layout" class = "layout"> </div>

Overall code:
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
<Style type = "text/css">
. Current {
Font-size: 20px;
}

. Over {
Display: none;
Position: absolute;
Top: 0;
Left: 0;
Width: 100%;
Height: 100%;
Background-color: # f5f5f5;
Opacity: 0.5;
Z-index: 1000;
}

. Layout {
Display: none;
Position: absolute;
Top: 40%;
Left: 40%;
Width: 20%;
Height: 20%;
Z-index: 1001;
Text-align: center;
}
</Style>
<Script type = "text/javascript">
Function showLoading ()
{
Document. getElementById ("over"). style. display = "block ";
Document. getElementById ("layout"). style. display = "block ";
}
</Script>
</Head>
<Body>
<Div class = "current"> <a href = "#" onclick = "showLoading ()"> Loading </a> </div>
<Div id = "over" class = "over"> </div>
<Div id = "layout" class = "layout"> </div>
</Body>
</Html>

Final effect:

I also saw another implementation method on the Internet. I felt that the idea was good. I used JS to constantly change the value of the html tag to load the prompt, based on his ideas, I implemented the following code:
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
<! -- <Script src = "Scripts/jquery-1.8.2.js"> </script> -->
<Style type = "text/css">
# Tb {
Width: 100%;
Height: 100%;
Line-height: 10px;
}

# Tb tr td {
Text-align: center;
}

. Progressbar {
Font-family: Arial;
Font-weight: bolder;
Color: gray;
Background-color: white;
Padding: 0px;
Border-style: none;
}

. Percent {
Font-family: Arial;
Color: gray;
Text-align: center;
Border-width: medium;
Border-style: none;
}
</Style>
<Script type = "text/javascript">
Var bar = 0;
Var step = "| ";
/*
* The first method is $ (document). ready (function (){.....});
*/
// $ (Function (){
// Progress ();
//});

/*
* Method 2
*/
// Window. onload = function (){
// Progress ();
//}

/*
* Method 3: Simulate $ (document). ready (function (){.....});
*/
(Function (){
Var ie = !! (Window. attachEvent &&! Window. opera );
Var wk =/webkit \/(\ d +)/I. test (navigator. userAgent) & (RegExp. $1 <525 );
Var fn = [];
Var run = function () {for (var I = 0; I <fn. length; I ++) fn [I] () ;};
Var d = document;
D. ready = function (f ){
If (! Ie &&! Wk & d. addEventListener)
Return d. addEventListener ('domainloaded', f, false );
If (fn. push (f)> 1) return;
If (ie)
(Function (){
Try {d.doc umentElement. doScroll ('left'); run ();}
Catch (err) {setTimeout (arguments. callee, 0 );}
})();
Else if (wk)
Var t = setInterval (function (){
If (/^ (loaded | complete) $/. test (d. readyState ))
ClearInterval (t), run ();
}, 0 );
};
})();

Document. ready (function (){

Progress ();

});


Function progress (){
Bar = bar + 2;
Step = step + "| ";
Document. getElementById ("percent"). value = bar + "% ";
Document. getElementById ("progressbar"). value = step;
If (bar <= 98 ){
SetTimeout ("progress ()", 100 );
}
}
</Script>
</Head>
<Body>
<Table id = "tb">
<Tr>
<Td>
<Input type = "text" size = "50" class = "percent" id = "percent"/> </td>
</Tr>
<Tr>
<Td>
<Input type = "text" size = "50" class = "progressbar" id = "progressbar"/> </td>
</Tr>
</Table>
</Body>
</Html>

Final effect:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.