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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Overview of JavaScript Event Calendars

Trending

  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Advancing Robot Vision and Control
  • Next Evolution in Integration: Architecting With Intent Using Model Context Protocol
  • Distributed Consensus: Paxos vs. Raft and Modern Implementations
  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 user avatar
Paul Underwood
·
Sep. 16, 16 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
21.2K Views

Join the DZone community and get the full member experience.

Join For Free

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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Overview of JavaScript Event Calendars

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!