Create jQuery Loading Animation to make your website Attractive

A loading animation is important to signal the user that the application is still doing something and the user should wait for an action to complete. This gets even more important when you use Ajax in your webapp.
Using the Ajax library jQuery it is very easy to create a loading animation that will be shown whenever an Ajax request is running.
First of all, you need an animated gif with a loading animation. You can pick a free gif and customize it at ajaxload.info
Include a div containing the image in every page of your webapp. Typically, you will add this div to a layout file or header that you include in every page.
<div id="loader" class="loader" style="display:none;"> <img id="img-loader" src="loader.gif" alt="Loading"/> </div>
The attribute style=”display:none;” makes the div invisible when the page is loaded.
You can style the div whatever you like using the class “loader”. I decided that I wanted the div to be centered in the middle of the screen on top of the rest of the page.
.loader { position: fixed; top: 50%; left: 50%; margin-left: -50px; /* half width of the loader gif */ margin-top: -50px; /* half height of the loader gif */ text-align:center; z-index:9999; overflow: auto; width: 100px; /* width of the loader gif */ height: 102px; /*hight of the loader gif +2px to fix IE8 issue */ }
To show the loader automatically when an Ajax request is running, add the following jQuery javascript code to every page by including it to a layout file or header.
<script type="text/javascript"> $(document).ready(function(){ $("#loader").bind("ajaxSend", function() { $(this).show(); }).bind("ajaxStop", function() { $(this).hide(); }).bind("ajaxError", function() { $(this).hide(); }); }); </script>
This will show the loading animation when an Ajax requests starts and hide it automatically when the Ajax request is complete or when an error occurs.
Additionally, it is possible to use the loader for normal, non-Ajax requests.
An example for this would be an ordinary file upload. If you use plain html, uploading a file does not trigger an Ajax request so your loader wouldn’t be visible automatically.
In your file upload form, give the submit button an html ID, e.g. buttom-upload. Now include the following jQuery javascript code to show the loader when the form is submitted:
<script type="text/javascript"> $(document).ready(function(){ $('#button-upload').click(function() { $('#loader').show(); }); }); </script>
You do not have to hide the loader, because when the request completes, a new page is rendered where the loader is not visible.
[ad#468x60-top-header]

Yep. Sounds good. Couldn’t agree more.
Dude, I spent HOURS looking into how not to have IE stop my animated .gif on Form Submit.
ARRRGGG!!!! Stupid IE!
Then I found this simple and effective solution laid out nicely with everything including .css
GOD BLESS YOU!
IE have many problem….
Its not working here yet. Wonder what am doing wrongly. Below is my loading.php page. Please could you help me find out what I have omitted? Thanks. Nice article.
.loader {
position: fixed;
top: 50%;
left: 50%;
margin-left: -50px; /* half width of the loader gif */
margin-top: -50px; /* half height of the loader gif */
text-align:center;
z-index:9999;
overflow: auto;
width: 100px; /* width of the loader gif */
height: 102px; /*hight of the loader gif +2px to fix IE8 issue */
}
$(document).ready(function(){
$(“#loader”).bind(“ajaxSend”, function() {
$(this).show();
}).bind(“ajaxStop”, function() {
$(this).hide();
}).bind(“ajaxError”, function() {
$(this).hide();
});
});
$(document).ready(function(){
$(‘#button-upload’).click(function() {
$(‘#loader’).show();
});
});
am tyring this loading script
Thanks Amir,
I slogged through a bunch of examples but yours was nice and clear and easy to understand.
$(“#loader”) – Shouldn’t this be $(“.loader”) as your tag attribute is class=”loader” not id=”loader” or have I missed something?
How does one use this? I know nothing about AJAX and I am confused by terms like “layout file or header”
I fully understand that the code is useful I just do not know how to use it. Is there a step-by-step that doesn’t assume the end-user has any programming experience?
Thank you dear. It is easy to understand. Work fine.
try this !
http://workwithphp.info/?p=8
thanks you verry much!
If you want to improve your jQuery Loading Animation, you may use Xeosoft Ajax Loader generator http://xeosoft.com/ajaxloader. There are many gif indicators with color, size and transparency customization. Xeosoft ajaxloaders are small and attractive.