I have a simple <div id="loader"></div>
in html that only show’s up when ajax is running in Jquery. Now This loader is only a simple spinning loading icon that i got from https://www.w3schools.com/howto/howto_css_loader.asp, but I want to make the background grey as well, so it’s easier to see the loading icon. But I couldn’t figure out on how to do this
Here is my css:
#loader {
position: absolute;
left: 50%;
top: 50%;
z-index: 1;
width: 120px;
height: 120px;
margin: -76px 0 0 -76px;
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
-webkit-animation: spin 2s linear infinite;
animation: spin 2s linear infinite;
}
@-webkit-keyframes spin {
0% {
-webkit-transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
}
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
and this is how I show my loader in jquery:
// Does the following if Ajax starts
$(document).ajaxStart(function() {
$("#loader").show();
});
// Does the following if Ajax stops
$(document).ajaxStop(function() {
$("#loader").hide();
$('#cancelBtn').removeAttr('hidden');
});
4
Answers
Just warp the loader in div and apply the below css rules.
If you want to add the colour to the background you just have to add one more property to the
#loader
:If you want to add a background to the full width of the page, you can add div:
with CSS properties:
You can also modify your jQuery code to toggle your animation:
Wrap your loader in a div, like so:
And add the following css:
If you don’t want to update your html, you could get a nice effect by using a drop shadow: