Create a simple loading animation using HTML, CSS, and JS.
- User
Create a simple loading animation using HTML, CSS, and JS.
- Assistant
Create a simple loading animation using HTML, CSS, and JS.
Creating a simple loading animation can be a fun task. Below, I'll guide you through creating a basic loading spinner using HTML, CSS, and JavaScript. This animation will consist of a rotating circle, which is a common choice for a loading indicator.
First, you need to set up the basic HTML structure. Create a container for the loading spinner.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Loading Animation</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="loader"></div>
<script src="script.js"></script>
</body>
</html>
Next, let's add some CSS to style the loading spinner. You'll use keyframes to create the spinning effect.
/* styles.css */
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.loader {
border: 16px solid #f3f3f3;
border-top: 16px solid #3498db;
border-radius: 50%;
width: 120px;
height: 120px;
animation: spin 2s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
For this simple spinner, JavaScript is optional since the animation is handled entirely by CSS. However, you might use JavaScript to dynamically show or hide the spinner based on your application's needs.
For example, you could add a button to toggle the visibility of the spinner:
// script.js
document.addEventListener("DOMContentLoaded", function() {
const loader = document.querySelector('.loader');
// Example function to show or hide the loader
function toggleLoader() {
loader.style.display = (loader.style.display === 'none') ? 'block' : 'none';
}
// Example usage: toggle the loader every 3 seconds
setInterval(toggleLoader, 3000);
});
div to act as the spinner..loader class applies styles to make the div look like a ring, using a border. The border-top property gives the top part of the border a different color, creating the "spinning" illusion when rotating.spin keyframe animation rotates the div infinitely.Now you have a simple loading animation that you can integrate into your web projects!
Not yet included in a published release.