How to Create and Display Loader Until the Page has Finished Loading using HTML, CSS, and JS

Page loader is an effective element on the web page that display and is visible to the user while the page is loading. When a web page is taking a long time to load, the loader animation creates a visual representation to help users to understand that the page is loading and will be available soon. Without a pre-loader, the user will see a blank white page while the page is in a loading state.

In this tutorial, we will show you how to create loader animation (with an image or text content) and display loader DIV until the web page has finished loading on the browser. You can make pre-loader elements easily using HTML and CSS. To handle the loader visibility you can use JavaScript.

The following example code snippet creates a page loader and display loader on the web page until the page has finished loading using HTML, CSS, and jQuery.

Create Loader element with HTML:

<div class="page-loader">
    <div class="spinner"></div>
    <div class="txt">Loading...</div>
</div>

Hide loader when the page has been loaded using jQuery:
Include jQuery library.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>

Create a custom JavaScript function to hide the loader element.

<script>
function hideLoader(){
  $('.page-loader').fadeOut('slow');
}
</script>

Pass the hideLoader() function in onload event and place it in the tag.

<body onload="hideLoader()">

Add style to pre-loader element with CSS:

/* Pre-loader CSS */
.page-loader{
    width: 100%;
    height: 100vh;
    position: absolute;
    background: #272727;
    z-index: 1000;
    .txt{
        color: #666;
        text-align: center;
        top: 40%;
        position: relative;
        text-transform: uppercase;
        letter-spacing: 0.3rem;
        font-weight: bold;
        line-height: 1.5;
    }
}

/* Spinner animation */
.spinner {
  position: relative;
  top: 35%;
  width: 80px;
  height: 80px;
  margin: 0 auto;
  background-color: #fff;
  border-radius: 100%;  
  -webkit-animation: sk-scaleout 1.0s infinite ease-in-out;
  animation: sk-scaleout 1.0s infinite ease-in-out;
}

@-webkit-keyframes sk-scaleout {
  0% { -webkit-transform: scale(0) }
  100% {
    -webkit-transform: scale(1.0);
    opacity: 0;
  }
}

@keyframes sk-scaleout {
  0% { 
    -webkit-transform: scale(0);
    transform: scale(0);
  } 100% {
    -webkit-transform: scale(1.0);
    transform: scale(1.0);
    opacity: 0;
  }
}

1 Comment

  1. Dhairya Said...

Leave a reply

keyboard_double_arrow_up