JQuery

Progress bar indicator using jQuery

Progress bar indicator using jQuery, someone asked me to explain?

In this article I will show you to develop progress bar indicator that notifies the user something is on progress loading. Clicking on the start button it shows a progress display and stop button stops the progress bar animation. It works based on the series of div elements.

Progess bar Html Code:

 <style type="text/css">
        .block {
            width: 50px;
            position: absolute;
            display: none;
        }
 
        .block1 {
            width: 10px;
            height: 10px;
            background-color: #0094ff;
            opacity: 1;
            float: left;
        }
 
        .block2 {
            width: 10px;
            height: 10px;
            background-color: #0094ff;
            opacity: 0.7;
            float: left;
        }
 
        .block3 {
            width: 10px;
            height: 10px;
            background-color: #0094ff;
            opacity: 0.5;
            float: left;
        }


        .block4 {
            width: 10px;
            height: 10px;
            background-color: #0094ff;
            opacity: 0.3;
            float: left;
        }
 
        .block5 {
            width: 10px;
            height: 10px;
            background-color: #0094ff;
            opacity: 0.1;
            float: left;
        }
 
        .container {
            width: 200px;
            height: 10px;
            border: solid 1px silver;
        }
    </style>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script type="text/javascript">
        $(document).ready(OnReady);
 
        function OnReady() {
            $("#start").click(function (event) {
                flag = true; ShowProgressIndicator(0);
                event.preventDefault();
            });
            $("#stop").click(function (event) {
                flag = false;
                event.preventDefault();
            });
        }
 
        function ShowProgressIndicator(dir) {
            $("#block").show();
            if (dir == 0) {
                $("#block").animate({ left: '160' }, 5000, OnAnimationComplete);
            }
            else {
                $("#block").animate({ left: '10' }, 5000, OnAnimationComplete);
            }
        }
        function OnAnimationComplete() {
            $("#block > div").each(function (index) {
                if ($(this).hasClass("block1")) {
                    $(this).removeClass();
                    $(this).addClass("block5");
                }
                else if ($(this).hasClass("block2")) {
                    $(this).removeClass();
                    $(this).addClass("block4");
                }
                else if ($(this).hasClass("block3")) {
                    $(this).removeClass();
                    $(this).addClass("block3");
                }
                else if ($(this).hasClass("block4")) {
                    $(this).removeClass();
                    $(this).addClass("block2");
                }
                else if ($(this).hasClass("block5")) {
                    $(this).removeClass();
                    $(this).addClass("block1");
                }
            });
            if (flag == true) {
                if ($("#block1").hasClass("block1")) {
                    ShowProgressIndicator(0);
                }
                else {
                    ShowProgressIndicator(1);
                }
 
            }
            else {
                $("#block").hide();
            }
        }
    </script>
<form id="form1" runat="server" style="border: 1px solid gray; width: 450px; height: 280px; padding-left: 15px;">
        <h2>Progress bar indicator using jQuery</h2>
        <div id="container" class="container">
            <div id="block" class="block">
                <div id="block5" class="block5"></div>
                <div id="block4" class="block4"></div>
                <div id="block3" class="block3"></div>
                <div id="block2" class="block2"></div>
                <div id="block1" class="block1"></div>
            </div>
        </div>
        <br />
        <button id="start">start</button>
        <button id="stop">stop</button>
    </form>

The outer div element contains five Div elements with the height of 10px and width 10px and set opacity for the fading looks. Initially I have set (display:none) for css class “block”.  When the user clicks the start button set flag variable to true and then calls the showprogressindicator() function by passing parameter 0. The stop button stops the loading bar animation and set flag variable to false. The flag variable determines whether to continue the progress bar animation or not.

Web page progress bar:

beautiful progress bar

 

Post your comments / questions