Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <h3>The Setup</h3> <p>Let's start by getting us a nice "loading" animation from <a href="http://ajaxload.info" rel="noreferrer">http://ajaxload.info</a> I'll be using <img src="https://i.stack.imgur.com/FhHRx.gif" alt="enter image description here"> </p> <p>Let's create an element that we can show/hide anytime we're making an ajax request:</p> <pre><code>&lt;div class="modal"&gt;&lt;!-- Place at bottom of page --&gt;&lt;/div&gt; </code></pre> <h3>The CSS</h3> <p>Next let's give it some flair:</p> <pre><code>/* Start by setting display:none to make this hidden. Then we position it in relation to the viewport window with position:fixed. Width, height, top and left speak for themselves. Background we set to 80% white with our animation centered, and no-repeating */ .modal { display: none; position: fixed; z-index: 1000; top: 0; left: 0; height: 100%; width: 100%; background: rgba( 255, 255, 255, .8 ) url('http://i.stack.imgur.com/FhHRx.gif') 50% 50% no-repeat; } /* When the body has the loading class, we turn the scrollbar off with overflow:hidden */ body.loading .modal { overflow: hidden; } /* Anytime the body has the loading class, our modal element will be visible */ body.loading .modal { display: block; } </code></pre> <h3>And finally, the jQuery</h3> <p>Alright, on to the jQuery. This next part is actually really simple:</p> <pre><code>$body = $("body"); $(document).on({ ajaxStart: function() { $body.addClass("loading"); }, ajaxStop: function() { $body.removeClass("loading"); } }); </code></pre> <p>That's it! We're attaching some events to the body element anytime the <code>ajaxStart</code> or <code>ajaxStop</code> 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.</p> <p>See it in action: <a href="http://jsfiddle.net/VpDUG/4952/" rel="noreferrer">http://jsfiddle.net/VpDUG/4952/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload