我們經(jīng)常在瀏覽網(wǎng)頁的時候會看到數(shù)據(jù)在加載時,,出現(xiàn)的LOADING提示。其實這個功能原理是很簡單的,,就是一個DIV遮蓋當(dāng)前頁面,然后Loading就在遮蓋DIV層上展示出來,,現(xiàn)在我們來動手實現(xiàn)一下,。
1.當(dāng)前頁面:
<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://files.jb51.net/file_images/article/201311/2013112931.gif" alt="" /></div>
整體代碼:
<!DOCTYPE html> <html xmlns="http://www./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://files.jb51.net/file_images/article/201311/2013112931.gif" alt="" /></div> </body> </html>
最終效果:
在網(wǎng)上還看到另外一種實現(xiàn)方式,感覺思路不錯,,就是利用JS不斷的改變html標(biāo)簽的value值,,達到加載提示的效果,根據(jù)他的思路我自己實現(xiàn)了下,,代碼如下:
<!DOCTYPE html> <html xmlns="http://www./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>
最終效果:
|