views:

791

answers:

2

I have two elements that shouldn't be active at the same time, so when one is toggled I fade the other out, however I would like to be able to fade the open element out and then bring the other one in. Is there a way to do this that isn't a hack?

<script ="text/javascript">

$(function() {
 $('#jlogin').click(function() {
  $('#login').toggle('fast');
  $('#reg').fadeOut('fast');
 });

 $('#jreg').click(function() {
  $('#reg').toggle('fast');
  $('#login').fadeOut('fast');
 });
});

</script>

That is my current script.

+2  A: 

Look at using the callback mechanism for fadeOut so you can chain the animations. The callback on the animation methods are called after the previous animation is complete.

 <script ="text/javascript">
    $(function() {
        $('#jlogin').click(function() {
           $('#reg').fadeOut('fast', function() {
               $('#login').toggle('fast');
           });
        });
        $('#jreg').click(function() {
            $('#login').fadeOut( 'fast', function() {
                $('#reg).toggle('fast');
            });
        });
     });
</script>
tvanfosson
any quick examples?
Oliver Stubley
I updated my example. I think I originally misunderstood what you were trying to do, but this example should fadeOut the element that should be removed then toggle the element that (presumably) will be shown. Should you be using show instead of toggle?
tvanfosson
I originally used toggle because I like the animation, will show be more beneficial? I need to be able to re-open the elements if needed.
Oliver Stubley
Also, tried the example and worked great after a bit of tweaking, I think yours had a hidden error somewhere.
Oliver Stubley
Yes. I see where I'm missing a closing paren and semi-colon. I've corrected it (though I still haven't tested it).
tvanfosson
If the element is always being shown on click, then you might want to use the version of toggle that takes a parameter indicating whether it should be shown, e.g. toggle(true), to guarantee the correct display.
tvanfosson
A: 

Thanks tvanfosson for you your 'chaining' demonstration. Great!

Bloke with headache