Simple implementation of JS Loading function

  • 2020-03-30 00:01:28
  • OfStack

We often see the LOADING prompt when the data is loaded when browsing the web. Actually this function principle is very simple, is a DIV cover the current page, and then Loading on the cover DIV layer show out, now we will start to implement.

1. Current page:


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

2. Mask layer:

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

3.Loading display layer:

<div id="layout" class="layout"><img src="//files.jb51.net/file_images/article/201311/2013112931.gif" alt="" /></div>

Overall code:

<!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="//files.jb51.net/file_images/article/201311/2013112931.gif" alt="" /></div>
</body>
</html>

Final effect:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201311/20131129094811.png ">

I have also seen another way of implementation on the Internet, and I think it is a good idea to use JS to constantly change the value value of HTML tags to achieve the effect of loading prompt. According to his idea, I realized the following code:


<!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 = "||";
        
        //$(function () {
        //    progress();
        //});
        
        //window.onload = function () {
        //    progress();
        //}
        
        (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>

Final effect:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201311/20131129094819.png ">

Related articles: