Introduction
Waiting for apps or webpages to load is boring for users. Playing animations helps users feel engaged when we load our content. It's cool to make loading animations more fun and beautiful. Here, we discuss how to add a loading animation to a webpage.
This article is meant for beginners in web development. Here we are taking an animation designed by Prathamesh Koshti using HTML + CSS and adding this animation as a loading page animation for the webpage. Also, we see how to add a gif animation as a loading animation, which is designed by Hassan Alkhateeb.
Below is how the loading page with animation designed by Hassan Alkhateeb looks
The second type of loading page with animation designed by Prathamesh Koshti will look like shown below.
Let's make it happen step by step.
Step 1. Open any IDE here. I use VS Code which is free and developed by Microsoft.
Step 2. Create a project. In VS Code Add a workplace folder, open Explorer, and add a new file index.html
Step 3. Code the home webpage you need in index.html. Here, for the purpose of simplicity, I'm using a simple webpage with only a title. The HTML+CSS of this is shown below.
<html>
<head>
<title>My Web Page</title>
</head>
<body style="background-color: black;">
<H1 style="color:white;text-align: center;padding: 300px;">My Website</H1>
</body>
</html>
Step 4. Do either step 4.1 or 4.2.
Step 4.1. Code the loading animation you need. The animation can be HTML+CSS+JS or a gif image.
Here as mentioned earlier I'm using an animation designed by Prathamesh Koshti. You can get it from Codepen. Copy the HTML to the body of index.html and CSS inside the style tag inside the head tag.
Step 4.2. Get the gif with the loading animation you need.
Here as mentioned earlier I'm using a gif designed by Hassan Alkhateeb. You can get the same from Behance. Download the image and save it in the project root folder. Add the below code after the opening of the body tag and replace the src value with the relative path of the gif you want to use.
<div class="container">
<img src="<relative path to the loading gif>" alt="">
</div>
Step 5. Add the below script to the end of the head tag. This will include jQuery v 3.5.1 and $(window).on('load', function () {}); to trigger the loading animation at the time of loading.
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(window).on('load', function () {
$(".container").fadeOut("slow");
});
</script>
Yola! Now you must be able to see the animation while the webpage loads. As the webpage is light you might not see the loading screen for a long time, since the loading finishes too soon.
Here is the full code if you use HTML+CSS animation designed by Prathamesh Koshti. Courtesy: Prathamesh Koshti Codepen.
<html>
<head>
<title>My Web Page</title>
<style>
:root {
--yellow: #feb60a;
--red: #ff0062;
--blue: #00dbf9;
--violet: #da00f7;
}
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #1a1940;
background-image: linear-gradient(180deg, rgba(0, 0, 0, 0.15) 0%, rgba(0, 153, 212, 0) calc(15% + 100px), rgba(0, 99, 138, 0) calc(85% + 100px), rgba(0, 0, 0, 0.15) 100%);
}
div.container {
display: flex;
justify-content: center;
align-items: center;
}
div>div {
width: 3vw;
height: 3vw;
border-radius: 100%;
margin: 2vw;
background-image: linear-gradient(145deg, rgba(255, 255, 255, 0.5) 0%, rgba(0, 0, 0, 0) 100%);
animation: bounce 1.5s 0.5s linear infinite;
}
.yellow {
background-color: var(--yellow);
}
.red {
background-color: var(--red);
animation-delay: 0.1s;
}
.blue {
background-color: var(--blue);
animation-delay: 0.2s;
}
.violet {
background-color: var(--violet);
animation-delay: 0.3s;
}
@keyframes bounce {
0%,
50%,
100% {
transform: scale(1);
filter: blur(0px);
}
25% {
transform: scale(0.6);
filter: blur(3px);
}
75% {
filter: blur(3px);
transform: scale(1.4);
}
}
</style>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(window).on('load', function () {
$(".container").fadeOut("slow");
});
</script>
</head>
<body style="background-color: black;">
<div class="container">
<div class="yellow"></div>
<div class="red"></div>
<div class="blue"></div>
<div class="violet"></div>
</div>
<H1 style="color:white;text-align: center;padding: 300px;">My Website</H1>
</body>
</html>
Here is the full code if you use the gif animation designed by Hassan Alkhateeb. Courtesy: Hassan Alkhateeb Behance.
<html>
<head>
<title>My Web Page</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(window).on('load', function () {
$(".container").fadeOut("slow");
});
</script>
</head>
<body style="background-color: black;">
<div class="container">
<img src="loading.gif" alt="">
</div>
<H1 style="color:white;text-align: center;padding: 300px;">My Website</H1>
</body>
</html>
Summary
Here, we learned how to design a loading page with animations in HTML+CSS format or GIF format. Also, we saw how to make it a loading page animation for our website. Now you can use animations in HTML+CSS format or GIF format to make your website look cool by adding a loader.
Hope it helped. Stay tuned for more articles on web development.