02.06.2013 Views

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Download from Wow! eBook <br />

};<br />

$("#go").toggle(function() {<br />

loader.show();<br />

loadingInterval = setInterval(function() {<br />

runLoader(); }, 1200);<br />

}, function() {<br />

loader.hide();<br />

clearInterval(loadingInterval);<br />

});<br />

[ 15 ]<br />

Chapter 1<br />

3. Save the file as loading.html in the main project folder (jquery-animation).<br />

Finally, we'll need to add a few basic styles to the example. Create a new file in your<br />

text editor and add to it the following code:<br />

#loader { margin:10px 0 0 36px; }<br />

#loader span {<br />

display:block; width:6px; float:left; margin-right:6px;<br />

border:1px solid #336633; position:relative;<br />

background-color:#ccffcc;<br />

}<br />

#loader .bar-0 { height:15px; bottom:-20px; }<br />

#loader .bar-1 { height:25px; bottom:-10px; }<br />

#loader .bar-2 { height:35px; margin-right:0; }<br />

4. Save this file in the css folder as loading.css.<br />

What just happened?<br />

The hardcoded onto the page is used to show and hide the loading animation.<br />

This is done purely for the purpose <strong>of</strong> this example. In an actual implementation, we'd show<br />

the loading animation at the start <strong>of</strong> a load operation, when new content was being added<br />

to the page for example, and then hide it again once the operation was complete.<br />

The first thing we do inside the outer function is set some variables. We create a new <br />

element as a container for the loader, using an object literal as the second argument to the<br />

$() (<strong>jQuery</strong>()) method to give it an id <strong>of</strong> loader. We then set its style to display:none<br />

with <strong>jQuery</strong>'s css() method so that it is not immediately visible.<br />

We also create a new element, which will be used as a template to create the<br />

three individual loading bars. We set its opacity to 0.2 (20% opaque), also using the css()<br />

method. <strong>jQuery</strong> normalizes this style for us so that it works correctly in Internet Explorer. The<br />

last variable, loadingInterval will be used to store the id <strong>of</strong> an interval so that we can<br />

clear the interval when we need to. We set this to null initially as the interval has not yet<br />

been set.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!