Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FadeIn after FadeOut

Tags:

jquery

I can't figure it out why my div won't fadeIn after FadeOut finishes.

Heres my Html:

<div class="header-container">
    <header class="wrapper clearfix">
        <ul class="nav nav-tabs">
            <li class="active">
                <a href="#tab1">Section 1</a>
            </li>
            <li>
                <a href="#tab2">Section 2</a>
            </li>
        </ul>
    </header>
</div>

<div class="main-container">
    <div class="main wrapper clearfix">

        <div class="tab-content">
                <div class="tab-pane active" id="tab1">
                    <p>I'm in Section 1.</p>
                </div>
                <div class="tab-pane" id="tab2">
                    <p>Howdy, I'm in Section 2.</p>
                </div>
        </div>

    </div> <!-- #main -->
</div> <!-- #main-container -->

My JS

jQuery(document).ready(function(){
    $('.nav-tabs a').click(function (e) {
      e.preventDefault();
      var href = $(this).attr('href'); // Select first tab
        $('.tab-pane').fadeOut(1000,function(){
            $(href).fadeIn(1000);
        });
    });

});

My Css

.tab-pane {
    display: none;
}

I made a jsfiddle:

http://jsfiddle.net/JohnnyDevv/hKq2K/1/

I made it simple as possible... Thanks in advance

like image 796
JonnyDevv Avatar asked Mar 27 '13 13:03

JonnyDevv


People also ask

Which of the following method is used to toggle between the fadeIn () method and fadeOut () method?

The fadeToggle() method toggles between the fadeIn() and fadeOut() methods. If the elements are faded out, fadeToggle() will fade them in.

What is the range of values for fading?

The jQuery fadeTo() method allows fading to a given opacity (value between 0 and 1).

What is fadeIn in JS?

The fadeIn() method gradually changes the opacity, for selected elements, from hidden to visible (fading effect). Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: This method is often used together with the fadeOut() method.

How does jQuery fadeOut work?

fadeOut() method animates the opacity of the matched elements. Once the opacity reaches 0, the display style property is set to none , so the element no longer affects the layout of the page. Durations are given in milliseconds; higher values indicate slower animations, not faster ones.


1 Answers

This will output the desired result you are expecting:

 $('.tab-pane').fadeOut(1000).promise().done(function(){
     $(href).fadeIn(1000);
 });

.promise() ensures to complete the fadeOut() first then when it completes .done() executes.

DEMO FIDDLE

like image 133
Jai Avatar answered Oct 25 '22 12:10

Jai