Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Run one function after another completes

Tags:

jquery

$('#tab-featured').tap(function(){
    $('.home-section').fadeOut(function(){
        $('#home-featured').fadeIn();
    });
});

I'm trying to use the code above to call the fadeIn() after the fadeOut() completes. The fadeOut() works fine. I've run functions after others complete before, but this time it's not working and for the life of me I can't figure out why.

Running jQuery latest from their CDN.

Code:

<div id="home-mid" class="column-mid">
    <div id="home-featured" class="home-main home-section">
        <!--- Some Code --->
    </div>

    <div id="home-2" class="home-main home-section">
        <!--- Some Code --->
    </div>

    <div id="home-3" class="home-main home-section">
        <!--- Some Code --->
    </div>

    <div id="home-4" class="home-main home-section">
        <!--- Some Code --->
    </div>

    <div id="home-5" class="home-main home-section">
        <!--- Some Code --->
    </div>

    <div id="home-tabs">
        <div id="tab-featured" class="home-tab"></div>
        <div id="tab-2" class="home-tab"></div>
        <div id="tab-3" class="home-tab"></div>
        <div id="tab-4" class="home-tab"></div>
        <div id="tab-5" class="home-tab"></div>
    </div>
</div>

Update:

Tried it with hide instead of fadeOut and it worked fine. Not sure why fadeOut isn't working.

like image 744
CoreyRS Avatar asked Jul 22 '26 18:07

CoreyRS


1 Answers

The first parameter of to the animation functions is a duration, the callback is the second one:

$('.home-section').fadeOut(250, function(){
    $('#home-featured').fadeIn();
});

Here's docs.

This could be a bug in fadeOut() / fadeIn() because your home-featured is also a home-section. Try working around it like this:

$('.home-section').fadeOut(function(){
    setTimeout(function () { $('#home-featured').fadeIn(); }, 50);
});
like image 104
Konstantin Dinev Avatar answered Jul 24 '26 09:07

Konstantin Dinev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!