One of the solutions is using the Sonic.js;
Documentation and examples:
https://github.com/padolsey/sonic.js/blob/master/README.md
How to use?
You can create a javascript function this way and add it to the file . js you prefer (remember to carry along with the file Sonic.js):
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, just depends on your creativity.
So completing your code could get + or - like this (do not forget to deal with errors that can return from ajax, otherwise the spinner will be there running endlessly).
In this case the code is all together, but you can declare getAjaxLoading in a file. js that is loaded everywhere, you choose the best implementation form 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>
Can the loading be inside the result div and after that delete? I know a javascript api that creates "spinners", very interesting by the way.
– Rafael Withoeft
I don’t know how it looks better. I thought of a div just for the 'load' that opens and closes before the result. But if it works within the result div itself has no problem
– GGirotto
This question is solved?
– Miguel Angelo
Yes, but I used a different method than the answers
– GGirotto