简单实现JS Loading功能

 我们经常在浏览网页的时候会看到数据在加载时,出现的LOADING提示。其实这个功能原理是很简单的,就是一个DIV遮盖当前页面,然后Loading就在遮盖DIV层上展示出来,现在我们来动手实现一下。

    1.当前页面:

   <div class="current"><a href="#" onclick="showLoading()">Loading</a></div>

    2.遮罩层:

  <div id="over" class="over"></div>

    3.Loading展示层:

<div id="layout" class="layout"><img src="http://images.cnblogs.com/cnblogs_com/qiuyan/477435/o_31.gif" /></div>

    整体代码:

技术分享

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
.current a {
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"><img src="http://images.cnblogs.com/cnblogs_com/qiuyan/477435/o_31.gif" /></div>
</body>
</html>

技术分享
 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <title></title>
 5     <style type="text/css">
 6         .current a {
 7             font-size: 20px;
 8         }
 9 
10         .over {
11             display: none;
12             position: absolute;
13             top: 0;
14             left: 0;
15             width: 100%;
16             height: 100%;
17             background-color: #f5f5f5;
18             opacity:0.5;
19             z-index: 1000;
20         }
21 
22         .layout {
23             display: none;
24             position: absolute;
25             top: 40%;
26             left: 40%;
27             width: 20%;
28             height: 20%;
29             z-index: 1001;
30             text-align:center;
31         }
32     </style>
33     <script type="text/javascript">
34         function showLoading()
35         {
36             document.getElementById("over").style.display = "block";
37             document.getElementById("layout").style.display = "block";
38         }
39     </script>
40 </head>
41 <body>
42     <div class="current"><a href="#" onclick="showLoading()">Loading</a></div>
43     <div id="over" class="over"></div>
44     <div id="layout" class="layout"><img src="http://images.cnblogs.com/cnblogs_com/qiuyan/477435/o_31.gif" /></div>
45 </body>
46 </html>
技术分享

    最终效果:

技术分享

      在网上还看到另外一种实现方式,感觉思路不错,就是利用JS不断的改变html标签的value值,达到加载提示的效果,根据他的思路我自己实现了下,代码如下:

 

技术分享

<!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 = "||";
/*
*第一种方式即 :$(document).ready(function(){.....});
*/
//$(function () {
// progress();
//});

/*
*第二种方式
*/
//window.onload = function () {
// progress();
//}

/*
*第三种方式模拟 $(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(‘DOMContentLoaded‘, f, false);
if (fn.push(f) > 1) return;
if (ie)
(function () {
try { d.documentElement.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>

技术分享
  1 <!DOCTYPE html>
  2 <html xmlns="http://www.w3.org/1999/xhtml">
  3 <head>
  4     <title></title>
  5     <!-- <script src="Scripts/jquery-1.8.2.js"></script>-->
  6     <style type="text/css">
  7         #tb {
  8             width: 100%;
  9             height: 100%;
 10             line-height: 10px;
 11         }
 12 
 13             #tb tr td {
 14                 text-align: center;
 15             }
 16 
 17         .progressbar {
 18             font-family: Arial;
 19             font-weight: bolder;
 20             color: gray;
 21             background-color: white;
 22             padding: 0px;
 23             border-style: none;
 24         }
 25 
 26         .percent {
 27             font-family: Arial;
 28             color: gray;
 29             text-align: center;
 30             border-width: medium;
 31             border-style: none;
 32         }
 33     </style>
 34     <script type="text/javascript">
 35         var bar = 0;
 36         var step = "||";
 37         /*
 38         *第一种方式即 :$(document).ready(function(){.....});
 39         */
 40         //$(function () {
 41         //    progress();
 42         //});
 43 
 44         /*
 45         *第二种方式 
 46         */
 47         //window.onload = function () {
 48         //    progress();
 49         //}
 50 
 51         /*
 52         *第三种方式模拟 $(document).ready(function(){.....});
 53         */
 54         (function () {
 55             var ie = !!(window.attachEvent && !window.opera);
 56             var wk = /webkit\/(\d+)/i.test(navigator.userAgent) && (RegExp.$1 < 525);
 57             var fn = [];
 58             var run = function () { for (var i = 0; i < fn.length; i++) fn[i](); };
 59             var d = document;
 60             d.ready = function (f) {
 61                 if (!ie && !wk && d.addEventListener)
 62                     return d.addEventListener(‘DOMContentLoaded‘, f, false);
 63                 if (fn.push(f) > 1) return;
 64                 if (ie)
 65                     (function () {
 66                         try { d.documentElement.doScroll(‘left‘); run(); }
 67                         catch (err) { setTimeout(arguments.callee, 0); }
 68                     })();
 69                 else if (wk)
 70                     var t = setInterval(function () {
 71                         if (/^(loaded|complete)$/.test(d.readyState))
 72                             clearInterval(t), run();
 73                     }, 0);
 74             };
 75         })();
 76 
 77         document.ready(function () {
 78 
 79             progress();
 80 
 81         });
 82 
 83 
 84         function progress() {
 85             bar = bar + 2;
 86             step = step + "||";
 87             document.getElementById("percent").value = bar + "%";
 88             document.getElementById("progressbar").value = step;
 89             if (bar <= 98) {
 90                 setTimeout("progress()", 100);
 91             }
 92         }
 93     </script>
 94 </head>
 95 <body>
 96     <table id="tb">
 97         <tr>
 98             <td>
 99                 <input type="text" size="50" class="percent" id="percent" /></td>
100         </tr>
101         <tr>
102             <td>
103                 <input type="text" size="50" class="progressbar" id="progressbar" /></td>
104         </tr>
105     </table>
106 </body>
107 </html>
技术分享

 

 

 

      最终效果:

技术分享

 

 

 

 

 

  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。