jQuery Sliding Effects

Sunday, July 8, 2012

jQuery has several methods that can be used to create a variety of sliding animation effects. The use of jQuery among web designers continues to be growing in popularity. With regard to basic sliding techniques the typical methods used are slideDown(), slideUp(), and slideToggle().



speedOptional values in milliseconds, "slow", "normal", and "fast".
easingIndicates which easing function (swing/linear) to use for the transition
callbackAn optional function to run after the method is completed

HTML Example

We will use the following HTML for the examples listed below.

<!DOCTYPE html>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
  // ... jQuery Code ...
<img id="img1" src="slideDown.png" />  
<img id="img2" src="slideUp.png" />
<img id="img3" src="toggle.png" /> ; 
<div id="div1"> <!-- Some Content --> </div>


In the following example, we will use the click event to trigger the slideDown(), slideUp(), and slideToggle() methods on the div element adjacent to the images. You will also notice that we modify the source attribute of img3 when the div element is either in a show or hidden state. Click on the various icons below to slide down, slide up, and slide toggle the div element.

<script type="text/javascript">
   $("#img3").attr("src", "collapse.png");
   $("#img3").attr("src", "expand.png");
 $("#img3").click(function () {
    var $img = $("#img3")
    if ($img.attr("src") == "expand.png") {
    $img.attr("src", "collapse.png");
    } else {
    $img.attr("src", "expand.png");

slide up slide down slide toggle
Click the icons to show, hide, or toggle this element.

Did you find the page informational and useful? Share it using one of your favorite social sites.

Recommended Books & Training Resources

JavaScript and jQuery: The Missing Manual Professional jQuery HTML CSS and JavaScript Editor