How can I create a “Please Wait, Loading…” animation using jQuery?

后端 未结 17 1995
长情又很酷
长情又很酷 2020-11-22 00:07

I would like to place a \"please wait, loading\" spinning circle animation on my site. How should I accomplish this using jQuery?

相关标签:
17条回答
  • 2020-11-22 00:36

    This would make the buttons disappear, then an animation of "loading" would appear in their place and finally just display a success message.

    $(function(){
        $('#submit').click(function(){
            $('#submit').hide();
            $("#form .buttons").append('<img src="assets/img/loading.gif" alt="Loading..." id="loading" />');
            $.post("sendmail.php",
                    {emailFrom: nameVal, subject: subjectVal, message: messageVal},
                    function(data){
                        jQuery("#form").slideUp("normal", function() {                 
                            $("#form").before('<h1>Success</h1><p>Your email was sent.</p>');
                        });
                    }
            );
        });
    });
    
    0 讨论(0)
  • 2020-11-22 00:37

    Place this code on your body tag

    <div class="loader">
    <div class="loader-centered">
        <div class="object square-one"></div>
        <div class="object square-two"></div>
        <div class="object square-three"></div>
    </div>
    </div>
    <div class="container">
    <div class="jumbotron">
        <h1 id="loading-text">Loading...</h1>
    </div>
    </div>
    

    And use this jquery script

    <script type="text/javascript">
    
    jQuery(window).load(function() {
    //$(".loader-centered").fadeOut();
    //in production change 5000 to 400
    $(".loader").delay(5000).fadeOut("slow");
    $("#loading-text").addClass('text-success').html('page loaded');
    });
    </script>
    

    See a full example working here.

    http://bootdey.com/snippets/view/page-loader

    0 讨论(0)
  • 2020-11-22 00:39

    Jonathon's excellent solution breaks in IE8 (the animation does not show at all). To fix this, change the CSS to:

    .modal {
    display:    none;
    position:   fixed;
    z-index:    1000;
    top:        0;
    left:       0;
    height:     100%;
    width:      100%;
    background: rgba( 255, 255, 255, .8 ) 
                url('http://i.stack.imgur.com/FhHRx.gif') 
                50% 50% 
                no-repeat;
    opacity: 0.80;
    -ms-filter: progid:DXImageTransform.Microsoft.Alpha(Opacity = 80);
    filter: alpha(opacity = 80)};
    
    0 讨论(0)
  • 2020-11-22 00:39

    jQuery provides event hooks for when AJAX requests start and end. You can hook into these to show your loader.

    For example, create the following div:

    <div id="spinner">
      <img src="images/spinner.gif" alt="Loading" />
    </div>
    

    Set it to display: none in your stylesheets. You can style it whatever way you want to. You can generate a nice loading image at Ajaxload.info, if you want to.

    Then, you can use something like the following to make it be shown automatically when sending Ajax requests:

    $(document).ready(function () {
    
        $('#spinner').bind("ajaxSend", function() {
            $(this).show();
        }).bind("ajaxComplete", function() {
            $(this).hide();
        });
    
    });
    

    Simply add this Javascript block to the end of your page before closing your body tag or wherever you see fit.

    Now, whenever you send Ajax requests, the #spinner div will be shown. When the request is complete, it'll be hidden again.

    0 讨论(0)
  • 2020-11-22 00:39

    I use CSS3 for animation

    /************ CSS3 *************/
    .icon-spin {
      font-size: 1.5em;
      display: inline-block;
      animation: spin1 2s infinite linear;
    }
    
    @keyframes spin1{
        0%{transform:rotate(0deg)}
        100%{transform:rotate(359deg)}
    }
    
    /************** CSS3 cross-platform ******************/
    
    .icon-spin-cross-platform {
      font-size: 1.5em;
      display: inline-block;
      -moz-animation: spin 2s infinite linear;
      -o-animation: spin 2s infinite linear;
      -webkit-animation: spin 2s infinite linear;
      animation: spin2 2s infinite linear;
    }
    
    @keyframes spin2{
        0%{transform:rotate(0deg)}
        100%{transform:rotate(359deg)}
    }
    @-moz-keyframes spin2{
        0%{-moz-transform:rotate(0deg)}
        100%{-moz-transform:rotate(359deg)}
    }
    @-webkit-keyframes spin2{
        0%{-webkit-transform:rotate(0deg)}
        100%{-webkit-transform:rotate(359deg)}
    }
    @-o-keyframes spin2{
        0%{-o-transform:rotate(0deg)}
        100%{-o-transform:rotate(359deg)}
    }
    @-ms-keyframes spin2{
        0%{-ms-transform:rotate(0deg)}
        100%{-ms-transform:rotate(359deg)}
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    
    
    <div class="row">
      <div class="col-md-6">
        Default CSS3
        <span class="glyphicon glyphicon-repeat icon-spin"></span>
      </div>
      <div class="col-md-6">
        Cross-Platform CSS3
        <span class="glyphicon glyphicon-repeat icon-spin-cross-platform"></span>
      </div>
    </div>

    0 讨论(0)
  • 2020-11-22 00:42

    Most of the solutions I have seen either expects us to design a loading overlay, keep it hidden and then unhide it when required, or, show a gif or image etc.

    I wanted to develop a robust plugin, where with a simply jQuery call I can display the loading screen and tear it down when the task is completed.

    Below is the code. It depends on Font awesome and jQuery:

    /**
     * Raj: Used basic sources from here: http://jsfiddle.net/eys3d/741/
     **/
    
    
    (function($){
        // Retain count concept: http://stackoverflow.com/a/2420247/260665
        // Callers should make sure that for every invocation of loadingSpinner method there has to be an equivalent invocation of removeLoadingSpinner
        var retainCount = 0;
    
        // http://stackoverflow.com/a/13992290/260665 difference between $.fn.extend and $.extend
        $.extend({
            loadingSpinner: function() {
                // add the overlay with loading image to the page
                var over = '<div id="custom-loading-overlay">' +
                    '<i id="custom-loading" class="fa fa-spinner fa-spin fa-3x fa-fw" style="font-size:48px; color: #470A68;"></i>'+
                    '</div>';
                if (0===retainCount) {
                    $(over).appendTo('body');
                }
                retainCount++;
            },
            removeLoadingSpinner: function() {
                retainCount--;
                if (retainCount<=0) {
                    $('#custom-loading-overlay').remove();
                    retainCount = 0;
                }
            }
        });
    }(jQuery)); 
    

    Just put the above in a js file and include it throughout the project.

    CSS addition:

    #custom-loading-overlay {
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        right: 0;
        background: #000;
        opacity: 0.8;
        filter: alpha(opacity=80);
    }
    #custom-loading {
        width: 50px;
        height: 57px;
        position: absolute;
        top: 50%;
        left: 50%;
        margin: -28px 0 0 -25px;
    }
    

    Invocation:

    $.loadingSpinner();
    $.removeLoadingSpinner();
    
    0 讨论(0)
提交回复
热议问题