views:

411

answers:

5

I'm looking to use jQuery to remove the need of using a GIF's to create a fairly simple animation.

What I want is an image to have four stages. 1) Nothing showing 2) The first part of the image 3) Second part of the image 4) Third part of the image - Start over

I was thinking I'd need to create the image stages then use a 'replace the current image with the next' technique with jQuery to create the animation. That could be pretty easy with callbacks.

The next part is to have this animation appear in several places after the previous animation had completed loading. Once again, could be easy with callbacks.

I was after some thoughts on this. Is it stupid not to just use GIF's? Or could this be done with jQuery?

A: 

First you can use the background of several div putting the part of image you need

css_background-position

Then you have to show the div as you need to be displayed depending on the type of animation you need.

andres descalzo
A: 

Use animated GIFs - this is what they are made for. I'm sure its possible to hack together a solution using JQuery, but you would pay a cost in added development time and complexity.

Additionally, you should recognize that by going with a customized JQuery/CSS solution, you are making a decision to tightly couple your animation to the page that its on. What if someone wants to include the animation on a different page? They would have to copy over a bunch of code instead of a single GIF file. What if someone wants to include it in an email or Power-point presentation? Can't be done...

Spike Williams
Gifs are UGLY and they are past, no alphamap, with solid matte on transparent backgrounds. with bg positioning of css sprites you can achieve much more than what you can achieve with gifs.
Sinan Y.
If having a transparent background on top of a non-solid background is an important consideration in this instance, then yes, go with the more powerful JQuery sprite solution. But the thing to recognize is that by doing so, you are paying a tax in extra complexity. Think of the poor maintenance coder who has to go into the code to change the graphic in a couple years. Is it fair to assume they will have a strong grasp of JQuery and css sprites? They might know that stuff, but I think it would be far kinder to let them just drop in a new GIF and be done with it.
Spike Williams
yep you're right it is more resource intensive than gifs, but we are heading to 2010, they should rather learn it :)
Sinan Y.
A: 

If it's going to be constant and not event-driven, I'd recommend using gifs.

If, however, the animation is in response to something happening on the page, or if you only want it to start after everything is loaded, then jquery is probably the way to go.

You should just be able to change the image source (or the position offset if you're using a single image) to change the image. Have a look at some "pause" options in jQuery to delay changes between images.

Untested example (derived from Simon's answer in the link above):

function chg1() { $('#myimage').attr('src', ''); }
function chg2() { $('#myimage').attr('src', 'image1src.jpg'); }
function chg3() { $('#myimage').attr('src', 'image2src.jpg'); }
function chg4() { $('#myimage').attr('src', 'image3src.jpg'); }

function StartAnimation() {
    setTimeout(chg1, 2000);  // blank after 2 seconds
    setTimeout(chg2, 4000);  // img 1 after 4 seconds
    setTimeout(chg3, 6000);  // img 2 after 6 seconds
    setTimeout(chg4, 8000);  // img 3 after 8 seconds
    setTimeout(StartAnimation, 8000);  // start again after 8 seconds
}

StartAnimation();  // start it off
Damovisa
looks like the calls to `animate` are going to stack up fine enough, but the changes to the `src`, since they are not occurring as a callback to your animations, are all going to fire instantly one after another?
Funka
@Funka - you're quite right, animate doesn't block. I'll modify the code.
Damovisa
Yes, @Funka is right. jQuery is asynchronous, so you will need to use callbacks to achieve this instead.
Jeremy Visser
Damovisa
I also would use a loop to switch the images... just set the image sources as an array, then itenerate through the array, using the jquery $data attribute on the image, defining the key of the array (the $el.data method accepts ints and other datatypes, not just strings).An example of this behavior can be seen on http://lookoutforsmartgrowth.org/'s home page image slider.
CodeJoust
Yes, there are definitely more elegant ways to do it.
Damovisa
You can check jQuery queue instead, http://docs.jquery.com/Core/queue
Sinan Y.
+1  A: 

Check this page for a demo for background animation with jquery and this for its tutorial.

Sinan.

Sinan Y.
A: 

Would it be possible to combine your four images into a sprite (1 real graphic with all four images included & non-overlapping)? From a performance standpoint, the client's browser is only having to download a single image in stead of making 4 requests for each of your four images.

Creating the animation would be as simple as using the technique suggested above by Damovisa, only doing it using some block element, setting this single image as a background and setting the size of the element to the size of one "tile" of the single image, and changing the background-position css property. I'm not completely sure about this (please correct me if I'm wrong), but it seems like a less expensive operation to move a background image around rather than actually swapping out four different images.

As far as portability and reusability, you could just create a jQuery plugin to be able to use it in several places.

jtrim