jQuery SlideToggle

jQuery slideToggle() Method

The jQuery slideToggle() method toggles between the slideDown() and slideUp() methods.

  1. If the elements have been slid down, slideToggle() will slide them up.
  2. If the elements have been slid up, slideToggle() will slide them down.

Syntax

$(selector).slideToggle(speed,callback);

  1. speed -It specifies the speed of the slideToggle effect. Default value is 400 milliseconds. Possible values:

    • milliseconds
    • slow
    • fast
  2. callback - A function to be executed after the slideToggle method is completed.

The following example demonstrates the slideToggle() method.

Example

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script> 
$(document).ready(function(){
$("#flipid").click(function(){
$("#panelid").slideToggle("slow");
});
});
</script>
<style> 
#flipid {
padding: 5px;
text-align: center;
background-color: red;
border: solid 1px green;
}

#panelid {
padding: 5px;
text-align: center;
background-color: green;
border: solid 1px green;
color:white;
}

#panelid {
padding: 50px;
}
</style>
</head>
<body>

<div id="flipid">Click to slide Toggle</div>
<div id="panelid">Tutorialstrend.com!</div>

</body>
</html>

Output

jquery slideToggle method

Now click on the Click to slide Toggle. It will slide up as following:

jquery slideToggle method

Now again click on the Click to slide Toggle. It will slide down as following:

Image3


Prev Next