• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / jQuery slideDown – How to Slide Down and Show an Element Using jQuery

jQuery slideDown – How to Slide Down and Show an Element Using jQuery

March 4, 2022 Leave a Comment

We can use jQuery slideDown() method to show a hidden div in HTML. The jQuery slideDown() method will slide the hidden element into view.

$('#div1').slideDown();

You can also enter milliseconds as a parameter in the slideDown() method to change the speed at which the slide down happens.

$('#div1').slideDown(1000);

The default speed is 400 milliseconds. In the example above, the div would slide down into view at 1000 milliseconds, or 1 second.

Let’s take a look at a very simple example below.


Let’s say we have the following HTML:

<style>#div3 { display: none; }</style>
<div id="div1">
  <div id="div2"></div>
  <div id="div3"></div>
</div>

To show #div3 we could use the jQuery slideDown() method

$('#div3').slideDown();

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery('#div3').slideDown();

Let’s take a look at another example below.

Using the jQuery slideDown Method to Start a Simple Menu Bar

In this example, we will build the start to a menu bar. The menu will work by hovering over a menu item, and then showing new menu items below the div you hovered over.

Here is the HTML and CSS setup:

<style>.example-menu{ height: 220px; } 
.menu-item-container { position: relative; }
#menu-item1 { position: relative; display: inline-block; } 
.menu-items { display: block; padding: 10px 14px; border: 1px solid #444; background: #fff; text-decoration: none; } 
.menu-sub-items { position: absolute; display: none; }
</style>
<div class="example-menu">
  <div class="menu-item-container">
    <div id="menu-item1" class="menu-items">About</div>
    <div id="menu-sub-item1" class="menu-sub-items">
      <a class="menu-items" href="#">Who We Are</a><a class="menu-items" href="#">Staff</a><a class="menu-items" href="#">Contact Us</a>
    </div>
  </div>
</div>

In our JavaScript code, we will make use of the jQuery hover() method first. When the user hovers over the “About” menu item, we will then use the jQuery slideDown() method to show the sub menu that was hidden.

The final part to this example will be to hide the sub-menu again when the user’s mouse leaves the menu. We will use the jQuery mouseleave() method to do this.

Once we leave the sub-menu, we will use the jQuery slideUp() method to hide the sub menu.

Here is the JavaScript code:

$("#menu-item1").hover(function(){
  $("#menu-sub-item2").hide();
  $("#menu-sub-item1").slideDown();
});

$(".menu-item-container").mouseleave(function(){
  $("#menu-sub-item1").slideUp(100);
});

The final code and output for this example of using the jQuery slideDown method to start a simple navigation bar is below:

Code Output:

About
Who We AreStaffContact Us

Full Code:

<style>.example-menu{ height: 220px; } 
.menu-item-container { position: relative; }
#menu-item1 { position: relative; display: inline-block; } 
.menu-items { display: block; padding: 10px 14px; border: 1px solid #444; background: #fff; text-decoration: none; } 
.menu-sub-items { position: absolute; display: none; }
</style>
<div class="example-menu">
  <div class="menu-item-container">
    <div id="menu-item1" class="menu-items">About</div>
    <div id="menu-sub-item1" class="menu-sub-items">
      <a class="menu-items" href="#">Who We Are</a><a class="menu-items" href="#">Staff</a><a class="menu-items" href="#">Contact Us</a>
    </div>
  </div>
</div>

<script>

$("#menu-item1").hover(function(){
  $("#menu-sub-item2").hide();
  $("#menu-sub-item1").slideDown();
});

$(".menu-item-container").mouseleave(function(){
  $("#menu-sub-item1").slideUp(100);
});

</script>

Hopefully this article has been useful for you to understand how the jQuery slideDown() method works.

Other Articles You'll Also Like:

  • 1.  Get nth Child with jQuery
  • 2.  Using jQuery to Move Element Before Another
  • 3.  jQuery text – Set the text of an HTML Element
  • 4.  jQuery siblings – Get the Sibling Elements of a Div
  • 5.  How to Check if Element Exists Using jQuery
  • 6.  jQuery has – Check if an Element Has Other Elements
  • 7.  Using jQuery to Change Image src
  • 8.  Using jQuery to Remove Class from an HTML Element
  • 9.  jQuery next – Get the Next Element of a Div
  • 10.  Using jQuery to Change Background Color

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy