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

后端 未结 17 1994
长情又很酷
长情又很酷 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:44

    With all due respect to other posts, you have here a very simple solution, using CSS3 and jQuery, without using any further external resources nor files.

    $('#submit').click(function(){
      $(this).addClass('button_loader').attr("value","");
      window.setTimeout(function(){
        $('#submit').removeClass('button_loader').attr("value","\u2713");
        $('#submit').prop('disabled', true);
      }, 3000);
    });
    #submit:focus{
      outline:none;
      outline-offset: none;
    }
    
    .button {
        display: inline-block;
        padding: 6px 12px;
        margin: 20px 8px;
        font-size: 14px;
        font-weight: 400;
        line-height: 1.42857143;
        text-align: center;
        white-space: nowrap;
        vertical-align: middle;
        -ms-touch-action: manipulation;
        cursor: pointer;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        background-image: none;
        border: 2px solid transparent;
        border-radius: 5px;
        color: #000;
        background-color: #b2b2b2;
        border-color: #969696;
    }
    
    .button_loader {
      background-color: transparent;
      border: 4px solid #f3f3f3;
      border-radius: 50%;
      border-top: 4px solid #969696;
      border-bottom: 4px solid #969696;
      width: 35px;
      height: 35px;
      -webkit-animation: spin 0.8s linear infinite;
      animation: spin 0.8s linear infinite;
    }
    
    @-webkit-keyframes spin {
      0% { -webkit-transform: rotate(0deg); }
      99% { -webkit-transform: rotate(360deg); }
    }
    
    @keyframes spin {
      0% { transform: rotate(0deg); }
      99% { transform: rotate(360deg); }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input id="submit" class="button" type="submit" value="Submit" />

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

    It is very simple.

    HTML

    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
    
    <body>
    
      <div id="cover"> <span class="glyphicon glyphicon-refresh w3-spin preloader-Icon"></span>Please Wait, Loading…</div>
    
      <h1>Dom Loaded</h1>
    </body>
    

    CSS

    #cover {
      position: fixed;
      height: 100%;
      width: 100%;
      top: 0;
      left: 0;
      background: #141526;
      z-index: 9999;
      font-size: 65px;
      text-align: center;
      padding-top: 200px;
      color: #fff;
      font-family:tahoma;
    }
    

    JS - JQuery

    $(window).on('load', function () {
      $("#cover").fadeOut(1750);
    });
    
    0 讨论(0)
  • 2020-11-22 00:52

    SVG animations are probably a better solution to this problem. You won't need to worry about writing CSS and compared to GIFs, you'll get better resolution and alpha transparency. Some very good SVG loading animations that you can use are here: http://samherbert.net/svg-loaders/

    You can also use those animations directly through a service I built: https://svgbox.net/icon-set/loaders. It allows you to customize the fill and direct usage (hotlinking) is permitted.

    To accomplish what you want to do with jQuery, you probably should have a loading info element hidden and use .show() when you want to show the loader. For eg, this code shows the loader after one second:

    setTimeout(function() {
      $("#load").show();
    }, 1000)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
    
    <div id="load" style="display:none">
        Please wait... 
        <img src="//s.svgbox.net/loaders.svg?fill=maroon&ic=tail-spin" 
             style="width:24px">
    </div>

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

    If you are using Turbolinks With Rails this is my solution:

    This is the CoffeeScript

    $(window).on 'page:fetch', ->
      $('body').append("<div class='modal'></div>")
      $('body').addClass("loading")
    
    $(window).on 'page:change', ->
      $('body').removeClass("loading")
    

    This is the SASS CSS based on the first excellent answer from Jonathan Sampson

    # loader.css.scss
    
    .modal {
        display:    none;
        position:   fixed;
        z-index:    1000;
        top:        0;
        left:       0;
        height:     100%;
        width:      100%;
        background: rgba( 255, 255, 255, 0.4)
                asset-url('ajax-loader.gif', image)
                50% 50% 
                no-repeat;
    }
    body.loading {
        overflow: hidden;   
    }
    
    body.loading .modal {
        display: block;
    }
    
    0 讨论(0)
  • 2020-11-22 00:54

    Per https://www.w3schools.com/howto/howto_css_loader.asp, this is a 2-step process with no JS:

    1.Add this HTML where you want the spinner: <div class="loader"></div>

    2.Add this CSS to make the actual spinner:

    .loader {
        border: 16px solid #f3f3f3; /* Light grey */
        border-top: 16px solid #3498db; /* Blue */
        border-radius: 50%;
        width: 120px;
        height: 120px;
        animation: spin 2s linear infinite;
    }
    
    @keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
    }
    
    0 讨论(0)
提交回复
热议问题