eozzy
eozzy

Reputation: 68690

Fade effect onclick (jQuery)

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!

Upvotes: 0

Views: 1336

Answers (2)

Yaakov Shoham
Yaakov Shoham

Reputation: 10548

Instead of

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

do

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

?

Upvotes: 1

rfunduk
rfunduk

Reputation: 30442

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;
  } );

Upvotes: 1

Related Questions