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?

Kane
  • 51
  • 1
  • 1
  • 4

6 Answers6

3

This is much easier and maintains chain-ability:

JQuery:

$(document).ready(function(){
    //rotator - delay, children
    $('#slideshow').rotator(50, 'img');
});

Markup:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script type="text/javascript" src="scripts/jquery.rotator.js"></script>

<style type="text/css">
#slideshow {
    position:absolute; //or whatever you need
    display:block;
    top:0px;
    left:0px;
}
#slideshow img {
    position:absolute;
    opacity:0;
}
</style>

<!-- SLIDESHOW -->
<div id="slideshow">
  <img src="images/1.png">
  <img src="images/2.png">
  <img src="images/3.png">
</div>

Plugin:

(function( $ ){
    $.fn.rotator = function(delay, child){
        //set curImage val
        var currImg = 0;
        var currIt = true;
        //set array of images
        var ss = $('#slideshow').children(child);
        var ssize = ss.size();
        setInterval(function() {
            if(currIt){
                $(ss[currImg]).css('opacity','1');
                currIt = !currIt;
            }else if (!currIt){
                $(ss[currImg]).css('opacity','0');
                $(ss[currImg+1]).css('opacity','1');
                currIt = !currIt;
                currImg++;
            }
            //reset
            if(currImg >= ssize){
                currImg = 0;
                $(ss[currImg]).css('opacity','1');
            }
        }, delay);
        return this;
    };
})(jQuery);
worked
  • 5,434
  • 5
  • 49
  • 75
  • This is a great solution, I am working on a smilar project and used this code, but ran into an issue. I exported a 9 second animation via image sequence and need to cycle through 300 images. This works great, however the slideshow lags. Is there a way to preload images so that the slideshow will start once the images preload? – TechyDude Nov 27 '12 at 22:42
1

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

Sinan.

Sinan
  • 10,875
  • 6
  • 34
  • 48
0

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
  • 14,451
  • 12
  • 60
  • 105
0

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
  • 29,764
  • 13
  • 43
  • 57
  • 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 Oct 27 '09 at 03:55
  • 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 Oct 27 '09 at 04:26
  • yep you're right it is more resource intensive than gifs, but we are heading to 2010, they should rather learn it :) – Sinan Oct 27 '09 at 21:18
0

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
Community
  • 1
  • 1
Damovisa
  • 18,263
  • 14
  • 62
  • 87
  • 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 Oct 27 '09 at 04:04
  • @Funka - you're quite right, animate doesn't block. I'll modify the code. – Damovisa Oct 27 '09 at 04:30
  • Yes, @Funka is right. jQuery is asynchronous, so you will need to use callbacks to achieve this instead. – Jeremy Visser Oct 27 '09 at 04:33
  • @Jeremy & Funka - I've updated the script. You were both right, that call is asynchronous. I've done it a different way now. – Damovisa Oct 27 '09 at 04:43
  • 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 Oct 27 '09 at 05:01
  • Yes, there are definitely more elegant ways to do it. – Damovisa Oct 27 '09 at 05:14
0

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
  • 3,455
  • 4
  • 29
  • 44