jQuery Fade

jQuery Fade Effects

jQuery provides powerful tools to fade elements in and out of visibility.

Fading is an elegant way to transition content without jarring the user layout.

Search engines also digest this smoothly without penalty.


The fadeIn() and fadeOut() Methods

The fadeIn() method is used to slowly display a hidden element.

The fadeOut() method is used to slowly hide a visible element.

Just like hiding, you can specify a speed parameter like "slow", "fast", or milliseconds.

Fade In and Out Example:

$("#fade-in-btn").click(function(){
  $("#box").fadeIn("slow");
});

$("#fade-out-btn").click(function(){ $("#box").fadeOut(2000); // Fades out over 2 seconds });


The fadeToggle() Method

The fadeToggle() method intelligently switches between fading in and fading out.

If the elements are faded out, fadeToggle() will fade them in.

If the elements are faded in, fadeToggle() will fade them out.

Fade Toggle Example:

$("button").click(function(){
  $("#box").fadeToggle("fast");
});

The fadeTo() Method

The fadeTo() method allows fading to a specific opacity level (transparency).

Unlike the other methods, it doesn't make the element disappear completely.

The opacity value must be a number between 0.00 and 1.00.

Fade To Example:

$("button").click(function(){
  // Fades the box to 30% opacity
  $("#box").fadeTo("slow", 0.3);
});

Exercise 1 of 2

?

Which method allows you to change an element's opacity to a precise percentage without fully hiding it?

Exercise 2 of 2

?

If an element is currently visible, what will calling fadeToggle() do to it?