I would like to place a "please wait, loading" spinning circle animation on my site. I'm having trouble to find a plugin for it, so here I am asking a weird question :)
Thank you for your help.
I would like to place a "please wait, loading" spinning circle animation on my site. I'm having trouble to find a plugin for it, so here I am asking a weird question :)
Thank you for your help.
You can grab an animated GIF of a spinning circle from Ajaxload - stick that somewhere in your website file heirarchy. Then you just need to add an HTML element with the correct code, and remove it when you're done. This is fairly simple:
function showLoadingImage() {
$('#yourParentElement').append('<div id="loading-image"><img src="path/to/loading.gif" alt="Loading..." /></div>');
}
function hideLoadingImage() {
$('#loading-image').remove();
}
You then just need to use these methods in your AJAX call:
$.load(
'http://example.com/myurl',
{ 'random': 'data': 1: 2, 'dwarfs': 7},
function (responseText, textStatus, XMLHttpRequest) {
hideLoadingImage();
}
);
// this will be run immediately after the AJAX call has been made,
// not when it completes.
showLoadingImage();
This has a few caveats: first of all, if you have two or more places the loading image can be shown, you're going to need to kep track of how many calls are running at once somehow, and only hide when they're all done. This can be done using a simple counter, which should work for almost all cases.
Secondly, this will only hide the loading image on a successful AJAX call. To handle the error states, you'll need to look into $.ajax
, which is more complex than $.load
, $.get
and the like, but a lot more flexible too.
Generally this is done by showing/hiding a div or two over the top of your content. You can get a fancy loading gif from http://www.ajaxload.info/ to get you started. Then you'll want to place a DIV on your page:
<div id="loading">
<p><img src="loading.gif" /> Please Wait</p>
</div>
You'll want this hidden by default, so you'd need to add this CSS:
#loading { display:none; }
You'd also want to setup the display for this too:
#loading { display:none; position:fixed; left:0; top:0; width:100%; height:100%;
background-image:url("transparentbg.png"); }
The file transparentbg.png
would be a 25x25 black PNG set to about 80% opaque. Next you would need a way to show and hide this with jQuery:
function showLoading() {
$("#loading").show();
}
function hideLoading() {
$("#loading").hide();
}
Now you can use this when you need to do something like querying an external page for data:
showLoading();
$.post("data.php", {var:"foo"}, function(results){
$("content").append(results);
hideLoading();
});
jQuery provides event hooks for when AJAX requests start and end. You can hook into these to show your loader.
For example, create the following div:
<div id="spinner">
<img src="images/spinner.gif" alt="Loading" />
</div>
Set it to display: none
in your stylesheets. You can style it whatever way you want to. You can generate a nice loading image at Ajaxload.info, if you want to.
Then, you can use something like the following to make it be shown automatically when sending Ajax requests:
$(document).ready(function () {
$('#spinner').bind("ajaxSend", function() {
$(this).show();
}).bind("ajaxComplete", function() {
$(this).hide();
});
});
Simply add this Javascript block to the end of your page before closing your body tag or wherever you see fit.
Now, whenever you send Ajax requests, the #spinner
div will be shown. When the request is complete, it'll be hidden again.
Along with what Jonathan and Samir suggested (both excellent answers btw!), jQuery has some built in events that it'll fire for you when making an ajax request.
There's the ajaxStart
event
Show a loading message whenever an AJAX request starts (and none is already active).
...and it's brother, the ajaxStop
event
Attach a function to be executed whenever all AJAX requests have ended. This is an Ajax Event.
Together, they make a fine way to show a progress message when any ajax activity is happening anywhere on the page.
HTML:
<div id="loading">
<p><img src="loading.gif" /> Please Wait</p>
</div>
Script:
$("#loading").ajaxStart(function(){
$(this).show();
}).ajaxStop(function(){
$(this).hide();
});