Creating a Full-Screen HTML/CSS Loader for AJAX Requests simple
Pate the below code in any File and save it as html file.
<!--------- CSS ---------->
<style>
#loader {
position: fixed;
z-index: 9999;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.7);
display: flex;
justify-content: center;
align-items: center;
display: none;
}
.spinner {
border: 4px solid rgba(0, 0, 0, 0.1);
border-top-color: #333;
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
</style>
<!--------- HTML ---------->
<div id="loader">
<div class="spinner"></div>
</div>
<button id="submit-btn">Submit</button>
<!--------- JS ---------->
<script type="text/javascript">
const loader = document.getElementById('loader');
const submitBtn = document.getElementById('submit-btn');
submitBtn.addEventListener('click', function() {
loader.style.display = 'flex';
// Perform AJAX request here
setTimeout(function() {
loader.style.display = 'none';
}, 2000); // Remove after 2 seconds to simulate AJAX response
});
</script>