views:

67

answers:

1

Doing some jquery animation. I have certain divs set up with an attribute of ani_seq='x' where x is 1 to 9, and then have a class assigned of 'animate'. I am using the following code, which works fine, and fades in each item in sequence:

    function my_animate( in_wrapper_id ) {
        $j("#" + in_wrapper_id + " .animate").hide(); // hide all items to be animated

        // animate all seq1 items --
        $j("#" + in_wrapper_id + " [ani_seq='1']").fadeIn( 1000, 
            function() { $j("#" + in_wrapper_id + " [ani_seq='2']").fadeIn( 1000,
                function() { $j("#" + in_wrapper_id + " [ani_seq='3']").fadeIn( 1000,
                    function() { $j("#" + in_wrapper_id + " [ani_seq='4']").fadeIn( 1000,
                        function() { $j("#" + in_wrapper_id + " [ani_seq='5']").fadeIn( 1000,
                            function() { $j("#" + in_wrapper_id + " [ani_seq='6']").fadeIn( 1000,
                                function() { $j("#" + in_wrapper_id + " [ani_seq='7']").fadeIn( 1000,
                                    function() { $j("#" + in_wrapper_id + " [ani_seq='8']").fadeIn( 1000,
                                        function() { $j("#" + in_wrapper_id + " [ani_seq='9']").fadeIn( 1000 ); });
                                    });
                                });
                            });
                        });
                    });
                });
            }); 
    }

The problem I have is, some items are not just fade-in. Some should slide in from the left or right. So, I can certainly write a custom function to do that. What I do not know how to do is set up the custom function so it works like the fadeIn() function in that it accepts a callback function that will be executed when the animation is done.

For example, lets say I i have a function like this (not sure this is the proper format):

function custom_animate ( in_time_in_ms, in_callback_function ) {
    // get class of element, and based on class perform either
    // a fade-in, or a slide-in from left, or a slide-in from right
    // then after animation is done, return control back to calling
    // function so it can resume
}

I would want to replace all of the fadeIn() calls in the first piece of code with custom_animate(), and then inside that function, determine what kind of animation to perform.

Can anyone help?

Thanks -

+1  A: 

Just pass in the in_callback_function as the callback argument to whatever animation function you end up calling in custom_animate().

$(something).fadeIn(1000, in_callback_function);

Also, your current code could really use a counter variable or something instead of repeated lines; repeating stuff isn't your job, it's the computer's job.

Here's an untested example that should get you going (fixing any typos is left as an exercise for the reader):

function my_animate(wrapper_id) {
    var wrapper = $("#" + wrapper_id);

    wrapper.find(".animate").hide();
    // use data() to store the next index in the wrapper itself
    wrapper.data("ani_seq", 1);

    my_animate_run(wrapper);
}

function my_animate_run(wrapper) {
    var seq = wrapper.data("ani_seq");

    var el = wrapper.find("[ani_seq='" + seq + "']");
    if (!el.length) // reached the last element
        return;

    wrapper.data("ani_seq", seq + 1); // update the sequence
    var next = function() { my_animate_run(wrapper); };

    // choose your type of animation here
    el.fadeIn(1000, next);
}
Matti Virkkunen
I like the concept - can you provide a simple example?
OneNerd
will try it out - looks great though if it works!
OneNerd
made some slight modifications for my purposes, and it worked great! Thanks very much.
OneNerd