One of the solutions is using sonic.js ;
Documentation and examples:
link
How to use?
You can create a javascript function like this and add it to the .js file you prefer (remember to load together the sonic.js file) in>:
var spinnerLoading;
function getAjaxLoading() {
if (typeof spinnerLoading === 'undefined') {
spinnerLoading = new Sonic({
width: 100,
height: 100,
stepsPerFrame: 1,
trailLength: 1,
pointDistance: .02,
fps: 30,
fillColor: '#05E2FF',
step: function(point, index) {
this._.beginPath();
this._.moveTo(point.x, point.y);
this._.arc(point.x, point.y, index * 7, 0, Math.PI*2, false);
this._.closePath();
this._.fill();
},
path: [
['arc', 50, 50, 30, 0, 360]
]
});
}
spinnerLoading.play();
return spinnerLoading.canvas;
}
Calling the spinner :
$('#resultado').html(getAjaxLoading());
You can create different spinners, it just depends on your creativity.
Then, by concluding your code, it could be + or - so (do not forget to handle the errors that can be returned from the ajax, otherwise the spinner will be spinning infinitely).
In this case the code is all together, but you can declare getAjaxLoading in a .js file that is loaded everywhere, you choose the best implementation for your case ...:
<script type="text/javascript">
jQuery(document).ready(function() {
var spinnerLoading;
function getAjaxLoading() {
if (typeof spinnerLoading === 'undefined') {
spinnerLoading = new Sonic({
width: 100,
height: 100,
stepsPerFrame: 1,
trailLength: 1,
pointDistance: .02,
fps: 30,
fillColor: '#05E2FF',
step: function(point, index) {
this._.beginPath();
this._.moveTo(point.x, point.y);
this._.arc(point.x, point.y, index * 7, 0, Math.PI * 2, false);
this._.closePath();
this._.fill();
},
path: [
['arc', 50, 50, 30, 0, 360]
]
});
}
spinnerLoading.play();
return spinnerLoading.canvas;
}
jQuery('#ajax_form').submit(function(e) {
e.preventDefault(); //Evitamos o comportamento padrão que é o submit do formulário
var dados = jQuery(this).serialize();
//Adicionando o LOADING
$('#resultado').html(getAjaxLoading());
$.ajax({
type: "POST",
url: "salvaregistro.php",
dataType: "JSON",
data: dados
}).done(function(data) {
$('#resultado').html(data).fadeIn("slow");
});
});
});
</script>