CSS:
/* Big Loading Animation */
#bigLoadingOverlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.6);
display: flex;
justify-content: center;
align-items: center;
z-index: 9999;
}
#loadingSpinner {
width: 150px;
height: 150px;
border: 10px solid #fff;
border-top: 10px solid transparent;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
/* small loading animation */
.loader {
width: 20px;
aspect-ratio: 1;
border-radius: 50%;
background: radial-gradient(farthest-side,#ffa516 94%,#0000) top/8px 8px no-repeat, conic-gradient(#0000 30%,#ffa516);
-webkit-mask: radial-gradient(farthest-side,#0000 calc(100% - 8px),#000 0);
animation: l13 1s infinite linear;
}
@keyframes l13 {
100% {
transform: rotate(1turn)
}
}
JavaScript:
function showSmallLoading(containerid) {
document.getElementById(containerid).classList.add("loader");
document.getElementById(containerid).style.display = "block";
}
function closeSmallLoading(containerid) {
try {
document.getElementById(containerid).style.display = "none";
}
catch (err) {
console.log(err);
}
}
function showBigLoading() {
// Create the overlay div
const overlay = document.createElement('div');
overlay.id = 'bigLoadingOverlay';
// Create the spinner
const spinner = document.createElement('div');
spinner.id = 'loadingSpinner';
// Append spinner to overlay
overlay.appendChild(spinner);
// Add to document body
document.body.appendChild(overlay);
// Add click event listener to close when clicked
overlay.addEventListener('click', closeBigLoading);
}
function closeBigLoading() {
const overlay = document.getElementById('bigLoadingOverlay');
if (overlay) {
overlay.remove();
}
}
How to use:
<span id="spanLoading"></span>
<script>
showBigLoading();
showLoading('spanLoading');
closeLoading('spanLoading');
</script>
<button type="button" onclick="showBigLoading();">Show Big Loading</button>
<button type="button" onclick="showSmallLoading('spanLoading');">Show Small Loading</button>
<button type="button" onclick="closeSmallLoading('spanLoading');">Close Small Loading</button>