JQuery_ Novice to Ninja - Earle Castledine [50]
You might be thinking that all these features would be fairly easy to add to our custom code—after all, we’re well on our way to becoming jQuery ninjas! If you’re feeling adventurous, open up the InnerFade plugin file to see how these features have been developed in there, which should give some idea about how you would implement them yourself.
As a departure from our image rotations, let’s have a look at rotating list items to create a simple news ticker, where a list of text links will be displayed randomly. To kick it off, we need to include the plugin in our page.
The ZIP file is linked at the very bottom of the plugin’s web page. It’s over 100KB—but don’t worry, most of that size consists of images used in the demos. The actual script weighs in at 8KB, and that’s without being compressed!
chapter_04/08_innerfade/index.html (excerpt)
Next, we’ll set up our containing element and the items we’d like to scroll. The plugin will treat all first-level children of the container we pass to it as fair game to cycle through. We’ll use an unordered list as the container, and the list items as the elements:
chapter_04/08_innerfade/index.html (excerpt)
News
When our document is ready, we use the plugin’s provided innerfade method on the list. There are a number of options available to customize the way this method works; we’re using a few here, and you should consult the plugin’s documentation to discover all of them. We’ll specify a slide effect, rather than a fade effect, to round out our news ticker style, and we’ll have the elements rotate at random:
chapter_04/08_innerfade/script.js (excerpt)
$('#news ul').innerfade({
animationtype: 'slide',
speed: 750,
timeout: 2000,
type: 'random'
});
And there we have it: a simple and cool effect for displaying news items. The InnerFade plugin is also perfectly suitable for image galleries like the ones we’ve already built, though you should be aware of one important difference. InnerFade handles all of the item hiding, showing, and positioning in its code—so without JavaScript all of the elements will be displayed (whereas in our custom code, we hid all but one in CSS). You’ll need to take this into consideration, and decide what you want the baseline experience of your site to be and how you’d like to enhance it with jQuery.
The Cycle Plugin
The Cycle plugin is a very mature, full-featured plugin that—like all the fading we have been doing—enables you to transition between elements in a container. Its completeness results in a comparatively hefty download (25KB for the complete minified version), but offers some impressive transition effects, well suited for displaying image galleries in a more interesting manner.
The setup should be very familiar to you now: download the plugin and add the JavaScript file to the head of your page. There are actually three different versions of the plugin contained in the download: a base version with only a slide transition (jquery.cycle.min.js, 16KB), a full-featured version with a wide range of available transitions (jquery.cycle.all.min.js, 25KB), and a stripped-down version with only the most basic options (jquery.cycle.lite.min.js, 4KB). For our example, we’ll use the full-fledged version for the sake of illustrating the available options.
We’ll start off with exactly the same markup we used for our previous slideshows. You can easily cross-fade images with the Cycle plugin, but given that it’s provided us with a number of fancier options, let’s try one and “shuffle” the images:
chapter_04/09_cycle_plugin/script.js (excerpt)
$('#photos').cycle({
fx: 'shuffle'