views:

487

answers:

2

I have this very basic tabbed block:

$('.tabbed-section .panel').hide();
$('.tabbed-section .panel:first').show();
$('.tabbed-section .tabs li:first').addClass('active');
$('.tabbed-section .tabs li a').click(function () {
    $('.tabbed-section .tabs li').removeClass('active');
    $(this).parent().addClass('active');
    var currentTab = $(this).attr('href');
    var tab_id = $(this).attr('href');
    $(this).closest('#hero').attr('class', 'clear ' + tab_id.replace('#', ''));
    $('.tabbed-section .panel').hide();
    $(currentTab).show();
    return false;
});

.. it works great, but can I add fade effect when the active tab changes? I think there's a plugin (innerfade) for it but I want to avoid using another plugin if possible.

Also, can the jQuery above be compacted further?

Thanks for your help!

+1  A: 

How about this?

$('.tabbed-section')
  .find('.panel').hide().end()
  .find('.panel:first').show().end()
  .find('.tabs li:first').addClass('active').end()
  .find('.tabs li a').click( function() {
    var el = $(this);
    $('.tabbed-section .tabs li').removeClass('active');
    el.parent().addClass('active');
    var currentTab = el.attr('href');
    el.closest('#hero').attr('class', 'clear ' + currentTab.replace('#', ''));
    $('.tabbed-section .panel').fadeOut( 'fast', function() {
      $(currentTab).fadeIn('fast');
    } );
    return false;
  } );
thenduks
Thanks, it almost works, just 2 things 1) the fade effects messes up the bottom elements on the page for a split second and 2) 3rd and 4th tab always 'blink' twice, 1st and 2nd don't.
Nimbuz
Right, well you didn't supply any markup so it's pretty impossible for me to experiment... In order to get this to look right you generally need to do just that, experiment. If running the animations at the same time _doesn't_ break your layout for whatever reason then by all means do that.
thenduks
+1  A: 

Instead of

$('.tabbed-section .panel').hide();
$(currentTab).show();

do

$('.tabbed-section .panel').fadeOut();
$(currentTab).fadeIn();

?

Y. Shoham
You probably don't want to do that since they'll both do their animations at the same time and that will likely break the layout since they'll both be visible for a time. Better to use the completion callback of the fadeOut, IMO.
thenduks
This works great actually, infact thenduks code break the layout for a split second.
Nimbuz