jQuery API

.fadeIn()

.fadeIn( [duration] [, callback] ) Returns: jQuery

Description: Display the matched elements by fading them to opaque.

  • version added: 1.0.fadeIn( [duration] [, callback] )

    durationA string or number determining how long the animation will run.

    callbackA function to call once the animation is complete.

  • version added: 1.4.3.fadeIn( [duration] [, easing] [, callback] )

    durationA string or number determining how long the animation will run.

    easingA string indicating which easing function to use for the transition.

    callbackA function to call once the animation is complete.

The .fadeIn() method animates the opacity of the matched elements.

Durations are given in milliseconds; higher values indicate slower animations, not faster ones. The strings 'fast' and 'slow' can be supplied to indicate durations of 200 and 600 milliseconds, respectively. If any other string is supplied, or if the duration parameter is omitted, the default duration of 400 milliseconds is used.

We can animate any element, such as a simple image:

<div id="clickme">
      Click here
    </div>
    <img id="book" src="book.png" alt="" width="100" height="123" />
    With the element initially hidden, we can show it slowly:
    $('#clickme').click(function() {
      $('#book').fadeIn('slow', function() {
        // Animation complete
      });
    });

Easing

As of jQuery 1.4.3, an optional string naming an easing function may be used. Easing functions specify the speed at which the animation progresses at different points within the animation. The only easing implementations in the jQuery library are the default, called swing, and one that progresses at a constant pace, called linear. More easing functions are available with the use of plug-ins, most notably the jQuery UI suite.

Callback Function

If supplied, the callback is fired once the animation is complete. This can be useful for stringing different animations together in sequence. The callback is not sent any arguments, but this is set to the DOM element being animated. If multiple elements are animated, it is important to note that the callback is executed once per matched element, not once for the animation as a whole.

As of jQuery 1.6, the .promise() method can be used in conjunction with the deferred.done() method to execute a single callback for the animation as a whole when all matching elements have completed their animations ( See the example for .promise() ).

Additional Notes:

  • All jQuery effects, including .fadeIn(), can be turned off globally by setting jQuery.fx.off = true, which effectively sets the duration to 0. For more information, see jQuery.fx.off.

Examples:

Example: Animates hidden divs to fade in one by one, completing each animation within 600 milliseconds.

<!DOCTYPE html>
<html>
<head>
  <style>
    span { color:red; cursor:pointer; }
    div { margin:3px; width:80px; display:none;
      height:80px; float:left; }
      div#one { background:#f00; }
      div#two { background:#0f0; }
      div#three { background:#00f; }
    </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <span>Click here...</span>

    <div id="one"></div>
    <div id="two"></div>
    <div id="three"></div>
<script>
      $(document.body).click(function () {
        $("div:hidden:first").fadeIn("slow");
      });
    </script>

</body>
</html>

Demo:

Example: Fades a red block in over the text. Once the animation is done, it quickly fades in more text on top.

<!DOCTYPE html>
<html>
<head>
  <style>
      p { position:relative; width:400px; height:90px; }
      div { position:absolute; width:400px; height:65px; 
        font-size:36px; text-align:center; 
        color:yellow; background:red;
        padding-top:25px; 
        top:0; left:0; display:none; }
        span { display:none; }
      </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>
        Let it be known that the party of the first part
        and the party of the second part are henceforth
        and hereto directed to assess the allegations
        for factual correctness... (<a href="#">click!</a>)
        <div><span>CENSORED!</span></div>

      </p>
<script>
        $("a").click(function () {
          $("div").fadeIn(3000, function () {
            $("span").fadeIn(100);
          });
          return false;
        }); 

      </script>

</body>
</html>

Demo:

Support and Contributions

Need help with .fadeIn() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .fadeIn()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • j2ee

    what is the difference between fadeIn() and show()

    • Anonymous

      show() will simply show your element without any animation whereas fadeIn() will use a fancy fade effect

      • http://www.learningjquery.com/ Karl Swedberg

        That’s only true if you don’t pass any arguments to .show(). If you include a speed argument, the .show() method will animate opacity, height, and width.

  • Román

    Is it possible to fadeIn an element from display:none to display:inline?

    • sauralph

      I had a smilar problem sometime ago, while inserting rows in a table. The display:block broke the table layout.

      My solution was to operate over the inserted DOM:

      $tbody = $(''tbody:first');
      $row = $(“<tr><td>SOME</td><td>TEST<td></tr>”);

      $tbody.append($row.hide().fadeIn());
      //this code displays to display:block

      $row.appendTo($tbody).hide().fadeIn();
      //this code displays to display:table-row

      • Robert

        Try setting css to inline after fade is complete:

        $element.fadeIn(‘slow’, function(){ $(this).css(‘display’, ‘inline’); });

  • Vincent

    I currently have a weird issue with the fadein animation. I am applying the fadein to a banner who appear partly behind a visual element. I am using the css z-index to do that. When the animation occure, the banner temporarly appear in front on my visual element.

    anyone can help me ?

  • Massimiliano

    and can delay the effect of certain second

  • Sergei S.

    What's the best way to fadeIn() from transparent to, say 80% opacity, rather than 100%?

    • http://twitter.com/rimmer333 Повод задуматься

      animate()

      • Kybosh

        couldnt you use the .fadeto() and then set the opacity?

  • Janine

    I am trying to find out whats wrong with my function now for over an hour. I still didnt find it out.
    Anyone can help me?
    Actually, .hide and .show works, but .fadein and .fadeout are instant.

    My code:

    $(“#n1″).fadeOut(“slow”);
    $(“#n1″).fadeIn(“slow”);

    (the element: Test)

    The span element is also inside of some other div and span elements, if thats important. Oh, and the span id=”n1″ is being created by javascript.

    • Janine

      Edit: I now solved it like this:

      $(#n1).animate({
      opacity: 0,
      height: 'toggle'
      }, 0);

      $(#n1).animate({
      opacity: 1,
      height: 'toggle'
      }, 1000);

      But fade would be better :(

  • Callum Taylor

    What's the best way to fade out a div but to keep the height of it, I.E. using visibility:hidden instead of display:none ?

    • cool_blue

      Put it inside another hidden div which doesn't fade hence the height remains even after the target div fades away!

    • cdmills

      I fade it down to a close-to-zero non-zero value like 0.01.

  • Kyle

    How do I fade it in without having to click? So it just does it on load.

    • http://www.learningjquery.com/ Karl Swedberg

      Bind the handler to the load event:

      $(window).bind('load', function() {
      $('someElement').fadeIn();
      });

    • Brbissoon

      I've had some problems with Karl's solution, but this is the one I've implemented and it works fine.

      $(function(){
      $('#div').hide();
      $('#div').fadeIn(1000);
      });

  • Jeroen

    Is there a way to prevent the backgroundcolor from showing during a fadeOut() + fadeIn()? I want to fadeOut an image and fadeIn another, but I can't get it working without seeing the background. I'm looking for somekind of 'fadeThrough'.

    This is what happens:
    1. I call fadeOut to hide an image inside a div (inside a div because there's text there also)
    * here the background is being exposed.
    2. I html() new stuff into the div.
    3. I fadeIn the div to show the new content

    What I want is:
    1. Put the new stuff 'behind' the to-be-removed div
    2. fadeOut the to-be-removed
    * this way the new stuff is shown immediately

  • Jquery

    If you fade an element out using hide() or fadeOut() you can't fade it back in by animating the opacity to 1. Hide() and fadeOut() change the display style to none and animate() can't animate the opacity of an element that has its display style property set to none.

    • Backslider

      Found this out just today, kinda sucks as I want to fade in and out elements more than once.

      • http://www.learningjquery.com/ Karl Swedberg

        But you can fade it back in using .fadeIn(), right?

  • Steviblaze

    When using fadeIn, I sometimes see a split-second full-opacity flicker of matched elements at the start of the fadeIn. Noticing this in Safari and Opera. The elements are display:none at page load. Is there a workaround for this? That flicker is taking a lot of steam out of my argument to convert existing animations from Flash to jQuery!

    • Frankuva

      Try fadeTo() instead. It doesn't mess with initial opacity values, it just moves towards the ending opacity value.

  • Chris

    Has any one noticed that when you use fadeIn (and fadeOut) with a transparent png they get a weird black box in IE? Is there a CSS fix to accompany?

    • kev

      add following to your css for the png-holding element:
      zoom: 1;

    • http://www.christophermanning.org/ Christopher Manning

      Convert the PNG's image mode from RGB to indexed to prevent the pixelation in IE during a fadeIn or fadeOut.

  • Chris

    Has any one noticed that when you use fadeIn (and fadeOut) with a transparent png they get a weird black box in IE? Is there a CSS fix to accompany?

  • Chris

    Has any one noticed that when you use fadeIn (and fadeOut) with a transparent png they get a weird black box in IE? Is there a CSS fix to accompany?

  • Chris

    Has any one noticed that when you use fadeIn (and fadeOut) with a transparent png they get a weird black box in IE? Is there a CSS fix to accompany?

  • Anonymous

    how can I change the time of transition, is there only slow and fast?

    • http://www.learningjquery.com/ Karl Swedberg

      You can pass in a number of milliseconds: $('something').fadeIn(2000);

  • Bencio

    I noticed a strange color text during transition.
    Original color is red, during transition seem to be green/dark-green.
    At the end of the fade original color gets back to red.
    Anyone could help me?

    • Marco

      I can bet you're using Firefox, fonts turn to green when opacity is changed like that.

      • http://www.bencio.net Bencio

        You win! I'm using firefox.
        Any suggestion to avoid this?
        (I could change effect into slideDown/Up, but I don't like it)

        • Me

          Might be a little late, but I think if you have a background-color set on the element you're fading, the text won't change color.

    • 524056680

      I can't

  • Frankuva

    Try fadeTo() instead. It doesn't mess with initial opacity values, it just moves towards the ending opacity value.

  • Karmrajsinh

    ie7 still problem with fadeIn() effect

    • http://www.google.com/profiles/109461199291622110643 Jason Capriotti

      What is the specific problem? Personally, I notice that the text displayed via fadeIn looks a little blocky or has artifacts around the edges. A simple way to fix this is to remove the inline filter style of the element you faded in.

  • http://zjfree.openid.35.com/ zjfree

    很方便!

  • Mark J. Handy

    is there an equivalent for visibility? I have a case where i've hidden a div be changing the visible state (i need to keep the div in float space).

    • http://www.learningjquery.com/ Karl Swedberg

      Try using .fadeTo() : http://api.jquery.com/fadeto/

      • http://tracker1.info/ Tracker1

        maybe… .fadeTo(0,0).css('visibility','visible').fadeTo(400,1)

  • quiff

    Is it possible to fade in an element that also uses .html(), for instance.

    $('#button').click(function(){

    $('#somediv').children().fadeOut('slow', function() {

    $('#somediv').html(“<div></div>”)

    });

    });

  • Prashant M

    fadein adds a style attribute called zoom in ie6. This disturbs the formatting wherein inline is taken as inline-block. Is there anyway to disable adding this style attribute? and will it affect anything else.

  • Stas

    I just want to fadeIn one string in <div> but can't understand how to do it during 15 mins. Documentation is awful!</div>

    • http://tracker1.info/ Tracker1

      if the string is only part of the contents of a div, wrap it in a span….

      ex: <span id=”myTextToShow”>This will be faded in</span>

      js: $('#myTextToShow').fadeIn();

      I think it is your understanding of CSS selectors and JavaScript that is at fault here.

  • Stas

    I just want to fadeIn one string in <div> but can't understand how to do it during 15 mins. Documentation is awful!</div>

  • Stas

    I just want to fadeIn one string in <div> but can't understand how to do it during 15 mins. Documentation is awful!</div>

  • Me

    Might be a little late, but I think if you have a background-color set on the element you're fading, the text won't change color.

  • Mbu725

    I noticed that the element on which you use fadeIn() must have the CSS display property set to “none”. I think it should be added to the documentation.

  • Kingpalan

    how do you get rid of the “click here………….” text after say 3 clicks?

    • http://twitter.com/idraki7 Idraki Muhamad

      make a count, and after the count=3, use remove() or hide() or anything similar

  • Gyubrush

    Is there any chance to do fadeIn (for ex. an image) from left to right instead of fading in whole image??

  • AlfaOmega08

    broken on firefox 4b10: instead of the background you see a black zone.