DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Trending

  • Designing Frontend Applications for Failure, Not Just Success
  • The Permission Was Valid. The Action Was Not Authorized.
  • Treat Salesforce Configuration Like Code: Release Discipline in a Copado Pipeline
  • Why Vulnerability Management Fails Without Reliable Asset Data

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. How to Reverse jQuery SlideUp and SlideDown

How to Reverse jQuery SlideUp and SlideDown

We take a look at how to reverse the purposes of jQuery's slideUp and slideDown methods. Read on for some code and explanations!

By 
Paul Underwood
·
Sep. 16, 16 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
21.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

By default jQuery slideDown will obviously slide the element down to display the contents and slideUp will slide the element up to hide the contents. What if you want to reverse these actions by using slideUp to show the contents and use slideDown to hide the contents.

You could use the jQuery UI slide function to change the direction of methods yourself but then you'll need to load the jQuery UI library with the jQuery library adding more weight to the application. Just as an example, you could use code similar to the below.

$('button').click(function () {
      $(this).hide('slide', { direction: "down" }, 1000);
});

The other option is to use the animate function to create something yourself by changing the height and margin-top of the element you want to slideUp.

The other option is to reverse the actions of the slideDown and slideUp by using CSS.

See the Pen amNxpN by Paul (@paulund) on CodePen.

How It Works

First, let's create a small test unit using two buttons that use slideDown and slideUp correctly to show the content.

<div id="normal">
    <button id="slideContentDown">Slide Down</button>
    <button id="slideContentUp">Slide Up</button>
    <div class="slide slideDown">
        Look this content slides down using the slideDown method.
    </div>
</div>

Next, we'll create the same two buttons and another content area that uses the slideDown method and slideUp method but reverse the methods so that they do the opposite.

<div id="reverse">
    <button id="reverseSlideContentDown">Reverse Slide Down</button>
    <button id="reverseSlideContentUp">Reverse Slide Up</button>
    <div class="slide slideUp">
        This content slides up using the slideDown method.
    </div>
</div>

With these scenarios created we can add the slide methods to the buttons.

$('#slideContentDown').on('click', function()
{
    $('.slideDown').slideDown();
});

$('#slideContentUp').on('click', function()
{
    $('.slideDown').slideUp();
});

$('#reverseSlideContentDown').on('click', function()
{
    $('.slideUp').slideDown();
});

$('#reverseSlideContentUp').on('click', function()
{
    $('.slideUp').slideUp();
});

Using the below CSS we can style the page to show the content.

@import 'https://fonts.googleapis.com/css?family=Lato';

body
{
    background: #3498db;
    color: #FFF;
    font-family: 'Lato', sans-serif;
    font-size: 150%;
}

button
{
    border: 2px solid #FFF;
    background: transparent;
    color: #FFF;
    cursor: pointer;
    font-size: 2rem;
    padding: 1rem 8rem;
    transition: background 0.4s;
    margin: 1rem 0;
}

button:hover
{
    background: #2980b9;
}

.slide
{
    background: #5ABEFF;
    padding: 4rem;
    margin: 1rem 0;
    display: none;
    width: 100%;
}

Below is the code we need to reverse the function, the important properties are position: absolute; and bottom: 0;. jQuery will know that this element has a bottom of 0, therefore it can't slide down so the functions are reversed and jQuery slides the element up to display the content.

#reverse
{
    padding-bottom: 10rem;
    position: relative;
}

.slideUp
{
    position: absolute;
    bottom: 0;
}
JQuery UI

Published at DZone with permission of Paul Underwood. See the original article here.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook