Initializing
The demo pages already include the following code. These steps are only necessary if you are building a page from scratch. Remove these references if you are not using this feature.
-
Include the common CSS
For easy readability of your HTML documents, it is preferable to load the animation stylesheet from an external file.
In the
<head>
of the HTML add the following line beforetheme.css
:<link href="assets/css/loaders/loader-typing.css" rel="stylesheet" type="text/css" media="all" />
This will provide all the necessary CSS for a loading animation.
You can find other available loading animations in the
assets/css/loaders
folder.If you are not using inline CSS, proceed to Step3.
While the above technique improves readability, it delays the loading of the page loader CSS. For faster loading of the CSS, include the animation CSS in-line in the
<head>
element beforetheme.css
. This ensures that the loading animation is the first element displayed on the page.Include the following in a
<style>
tag in the<head>
of the page. This is required for all page load animations.This needs to be done for each page where you wish to display a loading animation.
<style> @keyframes hideLoader{ 0%{ width: 100%; height: 100%; } 100%{ width: 0; height: 0; } } body > div.loader{ position: fixed; background: white; width: 100%; height: 100%; z-index: 1071; opacity: 0; transition: opacity .5s ease; overflow: hidden; pointer-events: none; display: flex; align-items: center; justify-content: center; } body:not(.loaded) > div.loader{ opacity: 1; } body:not(.loaded){ overflow: hidden; } body.loaded > div.loader{ animation: hideLoader .5s linear .5s forwards; } </style>
-
Include the css for the individual animation of your choice.
The below example uses the Typing animation.
Include the following
<style>
tag in the<head>
of the page.<style> /* Typing Animation */ .loading-animation { width: 6px; height: 6px; border-radius: 50%; animation: typing 1s linear infinite alternate; position: relative; left: -12px; } @keyframes typing { 0% { background-color: rgba(100,100,100, 1); box-shadow: 12px 0px 0px 0px rgba(100,100,100, 0.2), 24px 0px 0px 0px rgba(100,100,100, 0.2); } 25% { background-color: rgba(100,100,100, 0.4); box-shadow: 12px 0px 0px 0px rgba(100,100,100, 2), 24px 0px 0px 0px rgba(100,100,100, 0.2); } 75% { background-color: rgba(100,100,100, 0.4); box-shadow: 12px 0px 0px 0px rgba(100,100,100, 0.2), 24px 0px 0px 0px rgba(100,100,100, 1); } } </style>
-
Include the
.loader
and.loading-animation
HTML elementsAdd this snippet as the immediate first child of the
<body>
element.The
div.loader
is the full-screen element which will hide the page content. Thediv.loading-animation
is the element which is used as the animation. The CSS may use the:before
and:after
pseudo-elements of this<div>
to create more visible elements in the animation.<div class="loader"> <div class="loading-animation"></div> </div>
-
Include the script which will hide the animation when the page is finished loading.
Include the following
<script>
tag at the bottom of the page, before the closing</body>
tag.<script type="text/javascript"> window.addEventListener("load", function () { document.querySelector('body').classList.add('loaded'); }); </script>