Animate CSS display

danyo picture danyo · Jul 25, 2013 · Viewed 80.8k times · Source

Is there a way to animate the CSS display property in jQuery? I have the following:

$(".maincontent").css("display","block");

and want it to do something like this:

$(".maincontent").animate({"display": "block"}, 2500);

Answer

Derek Henderson picture Derek Henderson · Jul 25, 2013

Just use .show() passing it a parameter:

$(".maincontent").show(2500);

Edit (based on comments):

The above code fades in the element over a 2.5 second span. If instead you want a 2.5 second delay and then want the element to display, use the following:

$(".maincontent").delay(2500).fadeIn();

If, of course, you want a delay and a longer fade, just pass the number of milliseconds desired into each method.