jQuery slideToggle()

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

jQuery slideToggle()

jQuery slideToggle () method is used to toggle between slideUp() and slideDown() method. If the element is slide down, it will slide up the element and if it is slide up, it will slide down.

Syntax:

  1. $(selector).slideToggle(speed);  
  2. $(selector).slideToggle(speed, callback);   
  3. $(selector).slideToggle(speed, easing, callback);

speed: It specifies the speed of the delay. Its possible vales are slow, fast and milliseconds.

easing: It specifies the easing function to be used for transition.

callback: It is also an optional parameter. It specifies the function to be called after completion of slideToggle() effect.

Let's take an example to demonstrate jQuery slideToggle() effect.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>  
  5. <script>   
  6. $(document).ready(function(){  
  7.     $("#flip").click(function(){  
  8.         $("#panel").slideToggle("slow");  
  9.     });  
  10. });  
  11. </script>  
  12.  <style>   
  13. #panel, #flip {  
  14.     padding: 5px;  
  15.     text-align: center;  
  16.     background-color: #00FFFF;  
  17.     border: solid 1px #c3c3c3;  
  18. }  
  19. #panel {  
  20.     padding: 50px;  
  21.     display:none;  
  22. }  
  23. </style>  
  24. </head>  
  25. <body>  
  26. <div id="flip">Click to slide toggle panel</div>  
  27. <div id="panel">Hello javatpoint.com!   
  28. It is the best tutorial website to learn jQuery and other languages.</div>  
  29. </body>  
  30. </html>  

Test it Now

Output:

Click to slide toggle panel