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:


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:



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:

Full Code:




<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.

Categorized in:

jQuery,

Last Update: March 14, 2024