jQuery Stop Action Improves InnerFade Plugin

When animations go wrong it’s usually in the timing. Perhaps the browser can’t keep up with all that it’s asked to do or the operating system is bogged down by other tasks. In any case the lack of enough working memory or CPU cycles will have animations stacking up until there are enough resources to do all those fancy moves.

Some animations run continuously and others may be controlled by setting timers or stopping the animations at predetermined times. Other animations happen after some event has occurred, like when a user mouses over an element on the page or clicks a button.

Animations created with the jQuery InnerFade plugin run continuously, which may create a problem if the users’ computer resources aren’t up to the job.

It was noted that an InnerFade news ticker animation of links worked fine and as expected when the browser tab was left open and running. Working with another tab open and going back to the news ticker site, the news items got stacked onto one another so that the text was unreadable and the timing was double time.

Reloading the page or waiting for a couple of seconds to pass brought the expected behavior back. The animations caught up to where they should have been and the behavior continued as expected.

I’m not exactly sure why the animations stacked up in my page and not in the demo page by the plugin author, but I found a solution to this stacking problem.

jQuery lets us use a stop() action to clear the previously requested animations that haven’t finished yet. Optional parameters for the stop() action are clearQueue and gotoEnd, which are false by default.

To use the stop() action in this case, set both parameters to true. This will clear all animations in the queue and jump to the place where the action should be at now.

If it were important to catch up to the latest animation, use true for the optional gotoEnd parameter. When set to true, the gotoEnd parameter figures out what the screen should look like at the end of the present set of animations in the queue and goes there.

In the section of the code that responds to the slide or fade settings, I used the stop() action to clear the queue and catch up. The InnerFade plugin was altered on lines 93 and 96 (of the original script) by inserting .stop(true,true) just before the fadeOut() or slideUp() methods.

line 93...$(elements[last]).stop(true,true).slideUp(settings.speed);
line 96...$(elements[last]).stop(true,true).fadeOut(settings.speed);

Below is the portion of the original script that has been modified (on lines 3 and 6 here):

The script now works even better!

When two separate link lists were animated with the InnerFade plugin on the same page, the first one stacked up and the second list went blank when the stop() action was used with only the first parameter set to true, so it was important to set both the clearQueue and gotoEnd parameters to true.

The stop() action also comes in handy to clear the queue when event-driven animations get backed up from too much user input, like mousing over and out too fast.

An oddity in all this is that I couldn’t replicate the bad behavior (of the original plugin) inside of WordPress. The stacking up of animations was noticed in a static page outside of WordPress. To see the original and improved InnerFade plugins in action, go to the Good Ideas static page.

Is there some way that WordPress manages the scripts differently? I dunno, but the take away message here is to make sure and test your scripts to see how they act in various circumstances.

Javascript Plugin: InnerFade with jQuery

Fade elements in and out with the InnerFade javascript plugin. Its purpose is to fade “any element inside a container in and out” and it relies on jQuery to do its magic. InnerFade is a lightweight plugin of only 8 kb, so it won’t bog down your pages.

Any group of elements can be faded in and out with the InnerFade plugin as long as they are inside a container that is targeted by the first line of the script. Example elements that can be faded in and out are links in an unordered or ordered list, textual list items, or paragraphs in a div.

  • A. Eat Right – It’s the easiest medicine to take.
  • B. Buy Less Stuff – You don’t really need it, do you?
  • C. Recycle Everything – Save the Earth from humans.
  • D. Laugh Every Day – It helps to keep things in perspective.
  • E. Help One Another – Some day you’ll need a little help, so spread good karma.
  • F. Keep Smiling – It releases happy endorphins and makes you look great, too!
 

Target specific classes or ids for the right effect. Using the InnerFade plugin with jQuery is a simple way to show a countdown clock where one number fades out and the next one in the sequence fades in.

A news ticker, like the one above, can be created by fading in and out headlines for each news item. Each headline could be a link to the complete story. The headlines could be marked up as list items, like this list of good ideas:

  • A. Eat Right - It's the easiest medicine to take.
  • B. Buy Less Stuff - You don't really need it, do you?
  • C. Recycle Everything - Save the Earth from humans.
  • D. Laugh Every Day - It helps to keep things in perspective.
  • E. Help One Another - Some day you'll need a little help, so spread good karma.
  • F. Keep Smiling - It releases happy endorphins and makes you look great, too!

The easiest way to use the InnerFade plug is to accept the default settings, as we did above, and call it with a minimum of effort like so:

jQuery(document).ready(function($) {
    $('#good_ideas').innerfade();
  });

There are only a handful of options and changing them is fairly simple. The type of animation can be set to ‘fade’ or ‘slide’. The speed of the fading or sliding effects is set in milliseconds or with the strings ‘slow’, ‘normal’, or ‘fast’. Timeout is the time between animations in milliseconds. The type of slideshow is set using the strings ‘sequence’, ‘random’, or ‘random_start’. The height of the container element can be set in px, em, %.

Default Settings:

animationtype: fade
speed: normal
timeout: 2000
type: sequence
containerheight: auto
runningclass: innerfade

Now that you know you can call the javascript plugin and use it with the default settings, try changing the settings for different effects. For example, try sliding the elements slowly in and out of the 45px tall container in random order with a 3 second pause.

The InnerFade plugin handles all the hiding of elements until viewed, so without javascript enabled, the visitor will see everything on the page. Use a <noscript> method to offer alternative content to browsers with javascript turned off and put it in the html document just before the closing body tag.

InnerFade plugin can be used for simple slideshows too. The only caveat is to make sure images are of the same size or the bottom edges of larger pics will be seen behind the stack of images.

Generate Animated Loading… Images

We’ve all seen animated images on the screen as a video or slideshow is being downloaded and set up for us to view. Sometimes the word “loading…” is the animation itself. Sometimes there’s a horizontal bar or spinning world that alerts us to wait for the media to be loaded.

A few websites provide a way to generate your own animated loading image. They’re online and they’re free. If you’re tired of the same old circular or horizontal animated images, give the following sites a try.

Preloaders.net

It’s easy to generate a multitude of preloader images at preloaders.net, which is by far the most complete animated image generator available. All you do is choose the category of preloaders that you like and then select one preloader image from the spin box. From there you can alter several parameters of the image.

Several categories of preloaders differentiate preloaders.net from its competition, including three-dimensional, astronomy, people, religion signs, rectangular, smileys, social bookmarks, and zodiac signs.

You can choose between a GIF and an APNG image. Specify any color for the foreground and background colors or select a transparent background. Here’s an example animated clock made at preloaders.net: animated 3d clock

A slider lets you change the animation speed from slow to very fast. The slowest speed wasn’t quite slow enough and the fastest speed was way too fast, at least for my liking.

The dimensions of the preloader image can be specified as width and height values. A tick can be removed from the “constrain proportions” box to introduce some interesting effects to the animation. Advanced options include means to flip the image, reverse the animation, and inverse the colors.

LoadInfo.net

Loadinfo.net offers a variety of circular, clock-like and spinning images. The foreground and background colors can be any color you choose and the background could be transparent, too. Select one of the three thumbnail sizes (16×16, 24×24, 48×48) for your image and click on ‘generate’ to create it. animated ying-yang symbol If you like the image as presented on the screen, download the free image. GIFs created there did not have transparent backgrounds when I tried it, even though the ‘transparent’ feature was selected. The spinning ying-yang symbol seen here was given a white background for a little fake transparency.

ajaxload.info

Ajaxload.info provides a standard set of clocks, bars and flowers that can be modified with respect to foreground and background color. animated blue flower You’re stuck with the speed of the animation and the size of the image, but the background can be transparent. That’s not too many options, but sometimes a simple loading image is all that’s needed.

LoadingAPNG.com

LoadingAPNG is a free online animation generator that offers a few dozen circular and horizontal bar type of animated PNG. The foreground and background colors, width, height and transparency can be modified to suit. hearts horizontal bar animated

Chimply.com

Chimply offers several clocks or spinners and animated bars that you can modify with respect to colors, transparency and size. Unique to this free online image generator is that several images can be created and temporarily stored in a workspace on the website before wrapping them in a .zip file for easy download. animated spinning clock If you click on “Last generated images” near the bottom of the screen, you can see the images that other users have created. GIFs and SWFs can be downloaded.

CSSload.net

Timur Gafforov figured out how to present an animated image using pure CSS. Once HTML5 and CSS3 are more widely accepted and used by browsers everywhere, cssload.net is a source to remember. At the time of this writing CSS3 usage is in its infancy, so for now we’ll rely on small animated GIFs or APNG images to indicate that media is Loading….

CanvasLoader.js

Another way to show animated images on your site is to use pure javascript via CanvasLoader. Use the CanvasLoader Creator to build a script that will create your animated image on the fly. It’s a clock-like oval, spiral, square, rectangle or rounded rectangle that spins around in this animated image. Set the image diameter, density, range, frames per second, speed, and colors. Then, copy the code snippet to create a new CanvasLoader object using javascript.

Make sure to download the CanvasLoader UI library. The minified .js file is less than 6 kb. The custom snippet to produce an animated image is only 8 lines long.

Well, there we have seven sites that can help us make our websites stand out from the crowd. Take caution though, some of these animated images bring back visions of the early Internet, when scrolling marquees were thought to be cool. It’s best to avoid wacky-looking spinning objects unless they suit the style of your site. If you have a game site, maybe a twirling hat or an animated string of cats is appropriate for a loading image. For a more professional look stick to the more common horizontal bars, clocks or circular moving images and modify the colors to match your site.

jQuery Plugin Tips: ColorBox

ColorBox is described as “A lightweight customizable lightbox plugin for jQuery 1.3+”. As of this writing the ColorBox jQuery plugin is being actively supported as Version 1.3.19 was issued on 8 Dec 2011. It’s an open source work released with the MIT license and it’s one of the top ten jQuery plugins used around the world.

To use ColorBox first download the plugin, unpack the .zip file, and inspect the example directories. The index page in each example folder presents a series of links to slideshows with different features, such as elastic transitions, fade transitions, or using other content types. The colorbox directory contains a minified .js file and a non-minified JScript file.

ColorBox requires that its stylesheet be included in the <head> before its javascript source, which must be included after the javascript library. In the head of an HTML page call colorbox.css, jQuery library, and then any jQuery plugins — in that order. If you don’t call jquery.js before the plugin, nothing happens on the page where you’re wanting to view a lightbox or slideshow. You’ll see a “jQuery is not defined” error if you’re using the Firebug development tool.

We want the webpage to be fully constructed before running any scripts, so put <script>s near the end of the HTML, just before the closing body tag, </body>. That way, when elements are called for in the script, they will already be present.

You don’t want the scripts near the top of the document because of the way a web page is constructed. HTML is parsed by the browser until it comes across a script. When a script is encountered the parsing is paused and the script is then executed. If a script precedes the elements that it calls for, the script won’t work as intended.

Alternatively, the script could be wrapped in a ready() method like so:

Using the ready() function assures that the DOM is completely constructed before modifying it with the script.

ColorBox works on any jQuery selectable object. It’s a simple matter to create a slideshow by grouping images with the “rel” attribute or with a common class name, like so:

A simple, one-line script is all that’s needed to create a slideshow from the images grouped by their common class name, in this case .group1.

When the image link is clicked this script will center each image in a lightbox and shadow the rest of the page behind the image. Clicking on the page, or close button, will bring back the page as it was before clicking on the image.

To create a slideshow complete with previous, next and close buttons, add ‘rel=group1’ as a css key:value pair inside curly braces in the parentheses for the colorbox function.

Any number of key:value pairs can be added here to make your slideshow really stand out.

Other content types can be used with ColorBox, like HTML, Flash, video, or external webpages. Of course, that means you can let your imagination run wild and use ColorBox to your advantage in creating beautifully modern websites.