Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 56 ยท Effects and Animation

Fading and Sliding

Learn jQuery's fade and slide effects: fadeIn, fadeOut, fadeToggle, fadeTo, slideDown, slideUp, slideToggle, easing, callbacks and a working accordion.

The previous lesson showed .show(), .hide() and .toggle(), which animate width, height and opacity all at once. Often you want something calmer and more specific: an element that simply fades in, or a panel that rolls open like a window blind. jQuery has two small families of methods for exactly that. The fade family changes only opacity. The slide family changes only height (and the vertical padding and margin that go with it).

Both families are convenience wrappers around .animate(), so they share one calling convention: an optional duration, an optional easing name and an optional callback. Learn the pattern once and you know eleven methods.

Two families of effects

Fade Changes opacity only. Layout jumps at the end.
Slide Changes height only. Content below glides along.
1 / 2

The fade methods

.fadeIn() takes a hidden element and animates its opacity from 0 to 1. .fadeOut() does the reverse and, when the animation ends, sets display: none so the element no longer takes up space. .fadeToggle() picks whichever of the two applies.

JavaScript
$("#note").fadeIn();          // 400 ms, the default
$("#note").fadeOut(1000);     // one second
$("#note").fadeToggle("slow"); // "slow" is 600 ms

The default duration is 400 milliseconds. The two keywords are "fast" (200 ms) and "slow" (600 ms). Any other string is treated as the default, and any number is read as milliseconds.

A fade is only noticeable when the element actually changes state. Calling .fadeIn() on an element that is already visible does nothing visually, and calling .fadeOut() on one that is already hidden does nothing. Note that "hidden" means display: none. An element with visibility: hidden or opacity: 0 still occupies space and jQuery treats it as visible, so use display: none (or .hide()) as the starting state.

Fading to a specific opacity

.fadeTo(duration, opacity) is the odd one out. It requires both arguments and fades to any level between 0 and 1. It never hides the element, even at 0, and it does not unhide it.

JavaScript
$("img").fadeTo(300, 0.4);   // dim to 40%
$("img").fadeTo(300, 1);     // back to normal

Use it for "disabled" looks and hover dimming, where the element must keep its place in the layout.

Fade in, fade out, fade to

The slide methods

.slideDown() reveals a hidden element by growing its height from zero. .slideUp() shrinks it back and finishes with display: none. .slideToggle() chooses for you. They accept the same duration, easing and callback arguments.

JavaScript
$("#menu").slideDown(250);
$("#menu").slideUp("fast");
$("#menu").slideToggle();

Slide effects work best on block-level containers with no fixed height. Because the height is animated, content inside is clipped while it moves, and everything below the element is pushed along smoothly. That "pushing" is what makes slides the standard choice for menus, FAQ answers and collapsible filters.

If a panel stutters at the very end of a slide, check for vertical margins on the children. Margins that collapse through the container are not animated with it. Put padding on the panel itself, or add overflow: hidden to it, and the motion becomes smooth.

Easing

Easing describes how speed changes over time. jQuery ships with exactly two easing functions:

  • "swing" (the default) starts slowly, speeds up and slows down again at the end.
  • "linear" moves at a constant speed.
JavaScript
$("#panel").slideDown(500, "linear");

Names such as "easeOutBounce" come from plugins (jQuery UI includes them), so do not use them unless you have loaded one. An unknown easing name is not ignored: the animation throws an error on its first frame and never finishes. For fancier motion today, CSS transitions are usually the better tool; see From jQuery to Modern JavaScript.

Callbacks

The last argument is a function that runs when the animation is done. Inside it, this is the DOM element that just finished.

JavaScript
$("#toast").fadeIn(300, function () {
  $(this).delay(2000).fadeOut(300);
});

Two details trip people up:

  1. The callback runs once per matched element. If your selector matches five items, it fires five times. When you need one "everything finished" signal, use .promise() instead.
  2. Code placed on the line after the effect call runs immediately, not after the animation. Anything that depends on the end state belongs in the callback.
JavaScript
$("#box").fadeOut(500);
console.log("runs right now, the box is still fading");

$("#box").fadeOut(500, function () {
  console.log("runs after the fade is complete");
});

You can also pass a single options object, which is handy when you want to set a duration and a callback without remembering the order: .fadeIn({ duration: 300, easing: "linear", complete: fn }).

Building an accordion

An accordion shows one panel at a time. The recipe uses nothing new: a delegated click handler, .slideUp() on the others and .slideToggle() on the chosen panel. We also keep aria-expanded in step so screen reader users know the state, and each header is a real <button> so the keyboard works for free.

A slide-based accordion

The .stop(true, true) before each effect protects against fast clicking: without it, repeated clicks stack up animations that keep running after you stop. How and why is the subject of The Animation Queue.

Fade or slide?

Choose a fade when the element overlays other content (modals, tooltips, toasts) or when you do not want the layout to move. Choose a slide when the element is part of the page flow and its neighbors should make room smoothly. If you need both at the same time, combine them with .animate() in the next lesson.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does .fadeTo(300, 0) do to the element at the end?

    .fadeTo() only changes opacity. It never adds display: none, so the element keeps its space even when fully transparent.
  2. 2. Which two easing names are built into jQuery?

    jQuery core provides only swing (the default) and linear. Other names need a plugin such as jQuery UI.
  3. 3. You call $('.row').slideUp(200, fn) and the selector matches six rows. How many times does fn run?

    The complete callback is invoked for each matched element. Use .promise() when you need a single signal for the whole set.
  4. 4. Why should a fade start from display: none rather than visibility: hidden?

    jQuery decides whether an element is hidden by looking at display. An element hidden with visibility or opacity still occupies space and counts as visible.

Summary

  • Fade methods (.fadeIn(), .fadeOut(), .fadeToggle(), .fadeTo()) animate opacity; slide methods (.slideDown(), .slideUp(), .slideToggle()) animate height.
  • The default duration is 400 ms; "fast" is 200 and "slow" is 600. Easing is "swing" or "linear".
  • .fadeOut() and .slideUp() end with display: none; .fadeTo() never hides.
  • The callback runs once per matched element, with this set to that element. Code after the call does not wait.
  • An accordion is a delegated click, .slideUp() on the siblings and .slideToggle() on the target, plus aria-expanded.

Next, take full control of the motion in Custom Animations with .animate().