14

I use twitter bootstrap 3 applying .fade to show tabs with fade. That's fine except for the first tab's content is not shown for the first time:

http://jsfiddle.net/tVSv9/

<ul class="nav nav-tabs" id="myTab">
  <li class="active"><a href="#home" data-toggle="tab">Home</a></li>
  <li><a href="#profile" data-toggle="tab">Profile</a></li>
  <li><a href="#messages" data-toggle="tab">Messages</a></li>
</ul>

    <div id='content' class="tab-content">
      <div class="tab-pane fade active" id="home">
        <ul>
            <li>home</li>
            <li>home</li>
            <li>home</li>
            <li>home</li>
            <li>home</li>
            <li>home</li>
            <li>home</li>
        </ul>
      </div>
      <div class="tab-pane fade" id="profile">
        <ul>
            <li>profile</li>
            <li>profile</li>
            <li>profile</li>
            <li>profile</li>
            <li>profile</li>
            <li>profile</li>
            <li>profile</li>
        </ul>
      </div>
      <div class="tab-pane fade" id="messages">
          this is my message
      </div>
      <div class="tab-pane fade" id="settings"></div>
    </div>    

Where is the problem inside the code?

werva
  • 1,589
  • 5
  • 16
  • 19

3 Answers3

31

You need to add in class to your active tab content

The reason behind is there is predefined style in bootstrap css .fade.in {opacity: 1;} so if you are using .fade in tab then you need to add .in too

See this demo

HTML

<div class="tab-pane fade in active" id="home">
Vikas Ghodke
  • 6,602
  • 5
  • 27
  • 38
  • 1
    A much cleaner way to do so. Though I will keep my answer here as alternative. And it does give the 'fade in' effect the moment page is loaded. – JofryHS Sep 12 '13 at 06:47
  • I apologize for resurrecting an old post but is there any way to use fade without an active default tab? –  Apr 28 '15 at 13:37
2

It is a bit peculiar, but I could think of a quick hack to get this running.

$('.nav li.active').removeClass('active').find('a').trigger('click');

Put the above script inside $(document).ready( function() { ... })

What it does is basically remove your pre-defined active class, and re-enable it by mocking the 'click' event.

Fiddle: http://jsfiddle.net/jofrysutanto/tVSv9/2/

JofryHS
  • 5,804
  • 2
  • 32
  • 39
0

Also worth mentioning,

This will work:

$('#yourtabs a:first').tab('show');
$('.tab-pane:first').addClass('fade in');

This won't:

$('.tab-pane:first').addClass('fade in');
$('#yourtabs a:first').tab('show');

Otherwise your .fade will be hidden on init.

antk3
  • 15
  • 2