lnvrt
lnvrt

Reputation: 732

jQuery: sliding panels one after the other

I know this is a simple one for some of you, so please help me out! I have 3 panels (divs) which I want to slide one after the other from clicking a button. Sliding one panel is easy but how to slide multiple divs in order? Thanks.

Upvotes: 1

Views: 3348

Answers (2)

SolutionYogi
SolutionYogi

Reputation: 32233

Even though I really like Kane's solution, you may find it little hard to understand if you don't know 'anonymous functions' and arguments.callee features of JavaScript language.

Following also works:

$('#slideDiv').click(
    function(e)
    {
        e.preventDefault();
        var i = -1;
        var divList = $(".slide");
        var animationCallback = function() 
            { 
                if(++i < divList.length)
                    $(divList[i]).slideUp('slow', animationCallback);
                    //Replace 'slideUp' with any other animation of your choice. Make sure to pass 'animationCallback' as the last parameter. 
                    //e.g. you can do
                    //$(divList[i]).animate({ left : '300px'}, 100, 'linear', animationCallback);
            };

        animationCallback();
    }
 );

Demo Page →

EDIT:

Updated demo page. Added comment about how to use different animation.

Upvotes: 1

Kane Wallmann
Kane Wallmann

Reputation: 2322

Give the 3 div's a class ".slide_panel" this will loop through them and perform the animation on them.

$("#button").click(function()
{
    var i = -1;
    var arr = $(".slide_panel");
    (function(){
    if(arr[++i])
    $(arr[i]).animate({ left: "300px" }, 100, "linear", arguments.callee)
    })();
});

Demo Page →

Code

Upvotes: 8

Related Questions