skip to Main Content

I have a simple <div id="loader"></div> in html that only show’s up when ajax is running in Jquery. Now This loader is only a simple spinning loading icon that i got from https://www.w3schools.com/howto/howto_css_loader.asp, but I want to make the background grey as well, so it’s easier to see the loading icon. But I couldn’t figure out on how to do this

Here is my css:

#loader {
    position: absolute;
    left: 50%;
    top: 50%;
    z-index: 1;
    width: 120px;
    height: 120px;
    margin: -76px 0 0 -76px;
    border: 16px solid #f3f3f3;
    border-radius: 50%;
    border-top: 16px solid #3498db;
    -webkit-animation: spin 2s linear infinite;
    animation: spin 2s linear infinite;
}

@-webkit-keyframes spin {
    0% {
        -webkit-transform: rotate(0deg);
    }
    100% {
        -webkit-transform: rotate(360deg);
    }
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}

and this is how I show my loader in jquery:

// Does the following if Ajax starts
$(document).ajaxStart(function() {
    $("#loader").show();
});

// Does the following if Ajax stops
$(document).ajaxStop(function() {
    $("#loader").hide();
    $('#cancelBtn').removeAttr('hidden');
});

4

Answers


  1. Always use class selector it is a good practice and use max z-index value for any loading,modal etc to overlap other elements

    Just warp the loader in div and apply the below css rules.

    *{
      padding:0;
      margin:0;
      box-sizing:border-box;
    }
    
    .loader {
      width: 100vw;
      height: 100vh;
      background-color: lightgray;
      z-index: 999;/* max value*/
    }
    
    .loader > div {
        position: absolute;
        left: 50%;
        top: 50%;
        width: 120px;
        height: 120px;
        margin: -76px 0 0 -76px;
        border: 16px solid #f3f3f3;
        border-radius: 50%;
        border-top: 16px solid #3498db;
        -webkit-animation: spin 2s linear infinite;
        animation: spin 2s linear infinite;
    }
    
    @-webkit-keyframes spin {
        0% {
            -webkit-transform: rotate(0deg);
        }
        100% {
            -webkit-transform: rotate(360deg);
        }
    }
    
    @keyframes spin {
        0% {
            transform: rotate(0deg);
        }
        100% {
            transform: rotate(360deg);
        }
    }
    <div class='loader'>
      <div></div>
    </div>
    Login or Signup to reply.
  2. If you want to add the colour to the background you just have to add one more property to the #loader:

    background-color: #f3f3f3;
    

    If you want to add a background to the full width of the page, you can add div:

    <div class='content'>
     The content is not visible when animation is active!
    
      <div class="container">
        <div id="loader"></div>
      </div>
    </div>
    

    with CSS properties:

    .container {
      display: none;
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: #f3f3f3;
      z-index: 9999;
      justify-content: center;
      align-items: center;
    }
    

    You can also modify your jQuery code to toggle your animation:

    $(document).ajaxStart(function() {
      $(".container").css("display", "flex");
    });
    
    // Does the following if Ajax stops
    $(document).ajaxStop(function() {
      $(".container").css("display", "none");
    });
    
    Login or Signup to reply.
  3. Wrap your loader in a div, like so:

    <div class="loader-container">
       <div class="loader"></div>
    </div>
    

    And add the following css:

    .loader-container {
       display: flex;
       position: fixed;
       width: 100%;
       height: 100%;
       top: 0;
       left: 0;
       z-index: 10;
       background-color: grey;
    }
    
    .loader {
       position: relative;
       left: 50%;
       top: 50%;
       z-index: 11;
       width: 120px;
       height: 120px;
       margin: auto;
       border: 16px solid #f3f3f3;
       border-radius: 50%;
       border-top: 16px solid #3498db;
       -webkit-animation: spin 2s linear infinite;
       animation: spin 2s linear infinite;
    }
    
    Login or Signup to reply.
  4. If you don’t want to update your html, you could get a nice effect by using a drop shadow:

    #loader {
      position: absolute;
      left: 50%;
      top: 50%;
      z-index: 1;
      width: 120px;
      height: 120px;
      margin: -76px 0 0 -76px;
      border: 16px solid #f3f3f3;
      border-radius: 50%;
      border-top: 16px solid #3498db;
      -webkit-animation: spin 2s linear infinite;
      animation: spin 2s linear infinite;
      background: #aaa;
      box-shadow: 0 0 0 100vw #aaa;
    }
    
    @-webkit-keyframes spin {
      0% {
        -webkit-transform: rotate(0deg);
      }
      100% {
        -webkit-transform: rotate(360deg);
      }
    }
    
    @keyframes spin {
      0% {
        transform: rotate(0deg);
      }
      100% {
        transform: rotate(360deg);
      }
    }
    <div id="loader"></div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search