TExcelTAD
Home › jQuery › jQuery Slide

jQuery Slide

Slide effects animate an element's height (plus vertical padding and margin), so content rolls up or down like a blind. They are the classic way to build accordions, dropdown panels and "read more" sections.

The slide methods

Method What it does
.slideDown(duration, easing, callback) Reveals a hidden element by growing its height from 0
.slideUp(duration, easing, callback) Shrinks the height to 0, then sets display: none
.slideToggle(duration, easing, callback) Slides down if hidden, up if visible

The arguments are the same as for the other effects: a duration in ms (default 400) or "fast"/"slow", an optional easing, and an optional callback.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; max-width: 380px; }
    #toggle { width: 100%; padding: 8px; background: #0769ad; color: #fff; border: 0; cursor: pointer; }
    #panel { display: none; padding: 12px; background: #eef5fb; border: 1px solid #0769ad; border-top: 0; }
  </style>
</head>
<body>
  <button id="toggle">▼ Delivery information</button>
  <div id="panel">
    Orders placed before 2 pm ship the same day. Standard delivery takes 2–4 working days;
    express delivery arrives the next working day.
  </div>
  <p>Content below the panel moves smoothly.</p>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    $("#toggle").on("click", function () {
      $("#panel").slideToggle(300, function () {
        const open = $(this).is(":visible");
        $("#toggle").text((open ? "▲" : "▼") + " Delivery information");
        console.log("panel open:", open);
      });
    });
  </script>
</body>
</html>

Building an accordion

An accordion shows one section at a time. When a header is clicked, all other panels slide up and the clicked one toggles.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; max-width: 400px; }
    .acc h4 { margin: 0; padding: 8px 10px; background: #e3e9ef; border-bottom: 1px solid #fff; cursor: pointer; }
    .acc h4.open { background: #0769ad; color: #fff; }
    .acc div { display: none; padding: 8px 10px; background: #fafafa; }
  </style>
</head>
<body>
  <div class="acc">
    <h4>What is jQuery?</h4>
    <div>A JavaScript library that simplifies DOM work, events, effects and AJAX.</div>
    <h4>Is it free?</h4>
    <div>Yes — it is open source under the MIT license.</div>
    <h4>Does it still matter?</h4>
    <div>Many existing sites use it, so it is very useful to know.</div>
  </div>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    $(".acc h4").on("click", function () {
      const $head = $(this);
      const $body = $head.next("div");

      $(".acc div").not($body).slideUp(200);
      $(".acc h4").not($head).removeClass("open");

      $body.slideToggle(200);
      $head.toggleClass("open");
      console.log("clicked:", $head.text());
    });
  </script>
</body>
</html>

The key methods are .next("div") (the panel right after the header), .not(...) (everything except this one) and .toggleClass() to style the active header.

Slide or fade?

Effect Animates Good for
hide/show with duration width, height, opacity Small pop-in elements
fadeIn/fadeOut opacity Messages, images, overlays
slideDown/slideUp height, vertical padding/margin Panels, menus, accordions

A dropdown menu

Slides also work well for menus. This one opens on click and closes when you click anywhere else on the page.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; height: 240px; }
    .menu { position: relative; display: inline-block; }
    .menu ul { display: none; position: absolute; margin: 0; padding: 0; list-style: none;
               background: #fff; border: 1px solid #ccc; min-width: 140px; box-shadow: 0 4px 10px #0002; }
    .menu li { padding: 6px 10px; cursor: pointer; }
    .menu li:hover { background: #eef5fb; }
  </style>
</head>
<body>
  <div class="menu">
    <button id="open">Account ▾</button>
    <ul>
      <li>Profile</li>
      <li>Settings</li>
      <li>Sign out</li>
    </ul>
  </div>
  <p id="chosen">Nothing chosen.</p>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    const $list = $(".menu ul");
    $("#open").on("click", function (e) {
      e.stopPropagation();          // don't let the document handler close it at once
      $list.stop(true, true).slideToggle(150);
    });
    $list.on("click", "li", function () {
      $("#chosen").text("You chose: " + $(this).text());
      $list.slideUp(150);
    });
    $(document).on("click", function () {
      $list.slideUp(150);
    });
  </script>
</body>
</html>

Tip: Elements you plan to slide down should start hidden with CSS (display: none), not with a script after the page loads — otherwise users briefly see them before they disappear.

Note: Sliding animates height. If your element has a fixed CSS height, jQuery still restores it at the end, but avoid height: auto transitions in CSS on the same element, as they fight with jQuery's inline styles.

Test yourself

Which CSS property does a slide effect mainly animate?

  1. opacity
  2. width
  3. height
  4. transform

In an accordion, which call collapses every panel except $body?

  1. $(".acc div").slideUp().not($body)
  2. $(".acc div").not($body).slideUp()
  3. $body.siblings().slideDown()
  4. $body.slideToggle()

Why does the dropdown example call e.stopPropagation() on the button?

  1. To prevent the form from submitting
  2. So the click does not bubble to document, whose handler would close the menu immediately
  3. To stop the animation
  4. It is required by slideToggle