I would like to place a "please wait, loading" spinning circle animation on my site. How should I accomplish this using jQuery?
相关问题
- How to fix IE ClearType + jQuery opacity problem i
- jQuery add and remove delay
- Include empty value fields in jQuery .serialize()
- Disable Browser onUnload on certain links?
- how to get selected text from iframe with javascri
Most of the solutions I have seen either expects us to design a loading overlay, keep it hidden and then unhide it when required, or, show a gif or image etc.
I wanted to develop a robust plugin, where with a simply jQuery call I can display the loading screen and tear it down when the task is completed.
Below is the code. It depends on Font awesome and jQuery:
Just put the above in a js file and include it throughout the project.
Invocation:
You could do this various different ways. It could be a subtle as a small status on the page saying "Loading...", or as loud as an entire element graying out the page while the new data is loading. The approach I'm taking below will show you how to accomplish both methods.
The Setup
Let's start by getting us a nice "loading" animation from http://ajaxload.info I'll be using
Let's create an element that we can show/hide anytime we're making an ajax request:
The CSS
Next let's give it some flair:
And finally, the jQuery
Alright, on to the jQuery. This next part is actually really simple:
That's it! We're attaching some events to the body element anytime the
ajaxStart
orajaxStop
events are fired. When an ajax event starts, we add the "loading" class to the body. and when events are done, we remove the "loading" class from the body.See it in action: http://jsfiddle.net/VpDUG/4952/
Like Mark H said the blockUI is the way.
Ex.:
Obs.: I got the ajax-loader.gif on http://www.ajaxload.info/
Note that when using ASP.Net MVC, with
using (Ajax.BeginForm(...
, setting theajaxStart
will not work.Use the
AjaxOptions
to overcome this issue:I use CSS3 for animation
Jonathon's excellent solution breaks in IE8 (the animation does not show at all). To fix this, change the CSS to: