How to run jQuery fadeIn() and slideDown() simultaneously?

Jquery

Jquery Problem Overview


I've got a div with display: none; Now I want to show it using both: fadeIn and slideDown simultaneously.

$(this).slideDown({duration: 'slow', queue: false});
$(this).fadeIn({duration: 'slow', queue: false});

The div is selected properly. But when I trigger the effect, all it does is the slideDown. And if I just delete the slideDown I can see the fadeIn, so there is nothing wrong with the syntax. But why doesn't it trigger both animations?

Jquery Solutions


Solution 1 - Jquery

Use animate() instead of fadeIn():

$(this)
  .css('opacity', 0)
  .slideDown('slow')
  .animate(
    { opacity: 1 },
    { queue: false, duration: 'slow' }
  );

Solution 2 - Jquery

start with height:0px and opacity:0; filter: alpha(opacity = 0) then on the action do:

$(this).stop().animate({
    height: 200,
    opacity: 1
}, 350);

Change the height (i set to 200) and the duration (i set to 350) to whatever you want.

Solution 3 - Jquery

Here is my solution, you can use it as a jQuery plugin.

(function($) {
    'use strict';
    // Sort us out with the options parameters
    var getAnimOpts = function (a, b, c) {
            if (!a) { return {duration: 'normal'}; }
            if (!!c) { return {duration: a, easing: b, complete: c}; }
            if (!!b) { return {duration: a, complete: b}; }
            if (typeof a === 'object') { return a; }
            return { duration: a };
        },
        getUnqueuedOpts = function (opts) {
            return {
                queue: false,
                duration: opts.duration,
                easing: opts.easing
            };
        };
    // Declare our new effects
    $.fn.showDown = function (a, b, c) {
        var slideOpts = getAnimOpts(a, b, c), fadeOpts = getUnqueuedOpts(slideOpts);
        $(this).hide().css('opacity', 0).slideDown(slideOpts).animate({ opacity: 1 }, fadeOpts);
    };
    $.fn.hideUp = function (a, b, c) {
        var slideOpts = getAnimOpts(a, b, c), fadeOpts = getUnqueuedOpts(slideOpts);
        $(this).show().css('opacity', 1).slideUp(slideOpts).animate({ opacity: 0 }, fadeOpts);
    };
}(jQuery));

Now you can use it the same way you would use jQuery’s .fadeIn (or fadeOut) effect.

// Show
$('.alert').showDown('slow');
// Hide
$('.alert').hideUp('fast', function() {
    // Animation complete: '.alert' is now hidden
});

This will resize our element’s height with a fading effect.

It was [originally posted on my blog][1].

[1]: http://webmaestro.fr/blog/fade-a-bit-more-nicely-with-showdown-and-hideup/ "Fade a bit more nicely with .showDown() and .hideUp()"

Solution 4 - Jquery

$('.target')  					
	.hide()  
	.slideDown(500, 'swing')  
	.css('opacity', 0)  
	.animate({opacity: 1}, {queue: false, duration: 1000});

Solution 5 - Jquery

The more modern solution is to use values of 'show' and 'hide' when you want to combine animations:

$('.show').on('click', function () {
  $('.example').animate({
    opacity: 'show',
    height: 'show',
    marginTop: 'show',
    marginBottom: 'show',
    paddingTop: 'show',
    paddingBottom: 'show'
  })
})
$('.hide').on('click', function () {
  $('.example').animate({
    opacity: 'hide',
    height: 'hide',
    marginTop: 'hide',
    marginBottom: 'hide',
    paddingTop: 'hide',
    paddingBottom: 'hide'
  })
})

.example {
  background-color: blue;
  height: 200px;
  width: 200px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
  <button type="button" class="show">Show</button>
  <button type="button" class="hide">Hide</button>
</p>
<div class="example"></div>

Solution 6 - Jquery

        $(document).ready(function() {
    $("#test").bind("click", function() {
            setTimeout(function() {
            $('#slidedown').slideDown("slow");
        }, 500);
        $("#content").fadeOut(500);
        $(this).stop().animate({ "opacity": "1" }, "slow");
        });
    });

this is for fade out but i think it's what your after. please have a look at the example too: http://jsfiddle.net/oddacon/M44md/

Solution 7 - Jquery

It's possible now to use CSS3 transitions. It allows to achieve very flexible solutions.

HTML

<div id="btn">Click me</div>
<div id="hidden"></div>

CSS

#hidden {
    display: none; opacity: 0; height: 100px; background: red;
    transition: opacity 600ms ease-in-out 0s;
}
#hidden.opened {
    opacity: 1;
}

jQuery

$('#btn').click(function() {
    var div = $('#hidden');
    if ( ! div.is(':animated')) {
        div.slideToggle(600).toggleClass('opened');
    }
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniWebaholicView Question on Stackoverflow
Solution 1 - JquerybpierreView Answer on Stackoverflow
Solution 2 - JquerymoeView Answer on Stackoverflow
Solution 3 - JqueryEtienne BaudryView Answer on Stackoverflow
Solution 4 - JqueryDaniel GuerreiroView Answer on Stackoverflow
Solution 5 - JqueryzzzzBovView Answer on Stackoverflow
Solution 6 - JqueryOddaconView Answer on Stackoverflow
Solution 7 - JqueryArsen K.View Answer on Stackoverflow