.slideToggle() JQ Home  <<  Effects  <<  .slideToggle()

Sliding elements up or down.

Description

The .slideToggle() method uses a sliding motion to hide or show the matched set.

Syntax

Signature Description
.slideToggle( [duration] [, easing] [, callback] )Uses a sliding motion to hide or show the matched set.

Parameters

Parameter Description Type
durationA string or number determining the time the animation will run for.
default: 400
  • Durations are given in milliseconds and the higher the value, the slower the animation.
  • The string 'slow' can be used which equates to 600 milliseconds.
  • The string 'fast' can be used which equates to 200 milliseconds.
String or
Number
easingSpecify the speed at which the animation progresses at different points within the animation.
default: swing
  • The string 'swing' which is the default and causes the animation to animate faster at the beginning/end and slower in the middle.
  • The string 'linear' which causes the animation to progress at a constant pace.
String
callbackA function that fires once the animation is complete.
  • The callback is fired in the context of the current element in the set being animated, so the keyword this refers to that element.
  • The callback is fired once for each element in the matched set and can be used to string different animations together in sequence.
Function

Return

A jQuery object.

.slideToggle( [duration] [, easing] [, callback] ) Example  Effects  <<  Top

Uses a sliding motion to hide or show the matched set optionally providing a duration, and/or an easing and/or a callback function.

  • When used in its basic form with no parameters or incorrect parameters the .slideToggle() method defaults to sliding up or down the element(s) with a duration of 400 milliseconds which is the default.

In the example below when the left button is pressed we use 'swing' easing to animate the picture as we slide it up or down.

When the centre button is pressed we use a callback to alert when the image has been slid up or down.

When the right button is pressed we use a duration, easing and a callback to alert when the image is slid up or down.

a picture of pie a picture of pie a picture of pie

$(function(){
  $('#btn22').on('click', function() {
    $('#pie18').slideToggle('swing');
  });
 
  $('#btn23').on('click', function() {
    $('#pie19').slideToggle(function() {
      alert('Animation finished.');
    });
  });

  $('#btn24').on('click', function() {
    $('#pie20').slideToggle('slow', 'swing', function() {
      alert('Animation complete.');
    });
  });
});

Press the buttons below to action the above code:

               

Related Tutorials

jQuery Intermediate Tutorials - Lesson 7 - Fading & Sliding Effects