How To Make a Loader Spinner in CSS and Javascript?

Adding a loader spinner to your website improves user experience by indicating that content is loading. This guide will show you how to create a simple CSS loader and control its visibility using JavaScript.

Html Code

<div id="loader" class="loader">
<div class="spinner"></div>
</div>

<div id="content" style="display: none;">
<h1>Welcome to the MJ Info</h1>
<p>This is the main content of the page.</p>
</div>

Css Code

body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f7f7f7;
}

.loader {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.8);
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
}

.spinner {
border: 8px solid #f3f3f3;
border-top: 8px solid #3498db;
border-radius: 50%;
width: 50px;
height: 50px;
animation: spin 2s linear infinite;
}

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

JavaScript Code

window.addEventListener("load", function() {
  setTimeout(function() {
    const loader = document.getElementById('loader');
    const content = document.getElementById('content');
    loader.style.display = 'none'; // Hide loader
    content.style.display = 'block'; // Show content
  }, 3000); // 3000 milliseconds = 3 seconds
});

administrator

Leave a Reply

Your email address will not be published. Required fields are marked *