views:

679

answers:

2

THE SITUATION

i have a series of divs setup to contain various content:

<div id="main">
    <div id="about"></div>
    <div id="contact"></div>
    <div id="flickr"></div>
    <div id="twitter"></div>
</div>

I also have a nav which fades in and out specific divs from above (using either the animate or fade functions).


THE PROBLEM

I want to be able to check when all of the divs are hidden, when using the nav - so that i can trigger an event when all of the divs are hidden.

//Custom animation function.

jQuery.fn.fadeThenSlideToggle = function(speed, easing, callback) {
    if (this.is(":hidden")) {
     return this.slideDown({duration: 500, easing: "easeInOutCirc"}).animate({opacity: 1},700, "easeInOutCirc", callback);
    } else {
        return this.fadeTo(450, 0, "easeInOutCirc").slideUp({duration: 500, easing: "easeInOutCirc", complete: callback});
    }
};

//example nav items.

$('#toggleContact').click(function() {
    if (!$("#contact .tab").is(':animated')) {
     $('#contact .tab').fadeThenSlideToggle(); //custom animation function
    }
});

$('#toggleAbout').click(function() {
    if (!$("#about .tab").is(':animated')) {
     $('#about .tab').fadeThenSlideToggle();
    }
});

//example check to see if all divs are hidden.
if((($(":hidden", '#main').length == 3) { //3 not 4 due to delay :S
}

However, this is proving to be difficult as the check is always one div out due to the delay on the fade function updating the div to hidden.

Is there any robust way to check if all divs are hidden? Initialising the check when clicking on an item in the nav and taking into account that each div takes a certain duration to actually set to hidden once the item has been clicked?

Cheers.

EDIT- I'd like a solution that includes the 'check for hidden divs', not just where i should put it. Thanks.

+2  A: 

You can attach an event to the completion of the fade:

$("#MyDiv").fade(100,function(){
    DoSomething();
});
James Wiseman
I am making good use of the callback function at present. However, i'm not sure how to actually perform the check. What would you suggest? Using :hidden method as i am using? Or something else?
Scotty
+1  A: 

Add a callback function to your custom animation method, call this callback, if supplied, in the callback code for the animation to allow you to append some stuff to do after the animation is complete. I would depend on the state being what you expect once the animation is done.

$('#toggleContact').click(function() {
    if (!$("#contact .tab").is(':animated')) {
        $('#contact .tab').fadeThenSlideToggle( function() {
              // since this is the callback, we assume that the animation
              // has hidden all but one of the tabs
              ... more stuff to do after the animation is complete ...
        });
    }
});
tvanfosson
So would you suggest putting the check to see whether all the divs concerned are hidden inside that callback?What if i was already passing stuff into the callback? Can i just add it onto the end?
Scotty
Surely the results of the animation are the condition that you are testing for. In the callback you should be able to assume that all but one of the divs is hidden. Perhaps, you just need to add a callback parameter to your custom function so that you can add code in the current method. I'll update my answer with this.
tvanfosson
The toggleContact doesn't affect any other div but the corresponding contact div. I have a series of toggle functions that only fade in/out the corresponding div.So clicking about in nav will hide and show the about div etc. I need to be able to check when all of the divs have been toggled to fade out so that all the divs are hidden, if that makes sense?I would have thought the check would need to be more global than just situated inside the callback. Unless i have the same code running in each callback item in the nav?
Scotty
But then would it make more sense to put that code in the custom animation function?
Scotty
Having the code in a callback to your custom animation is more flexible, since then you could do something different depending on which menu item is being manipulated. It seems to me that the check should happen as part of the click event handling, not as part of the set up like you have it now. Right now you are only checking it when you are setting up the click handlers, not when the click event happens.
tvanfosson