It is very easy to set effects in application using jQuery. You can show hide and toggle elements with animation on any event in jQuery.
.show() Method
The show() method display the hidden, selected elements. and it works on element hidden with display:none in CSS and jQuery methods.
Syntax$(selector).show(speed,callback)
.hide() Method
The hide() method hide the selected elements. and it is similar to display:none in CSS.
Syntax$(selector).hide(speed,callback)
An example of show and hide menu
In this example we have two button sequentially when i click show menu button then it show menu if hidden and when i click hide menu then it hide menu if shown.
I pass the additional speed parameter in show hide method to run the animation.
- <!DOCTYPE html>
- <html>
- <head>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
- <script>
- $(document).ready(function() {
- $("#show_menu").click(function () {
- $( ".list_container" ).show(3000);
- });
- $("#hide_menu").click(function () {
- $( ".list_container" ).hide(3000);
- });
- });
- </script>
- </head>
- <body>
- <div class="list_container" style="display:none">
- <ul>
- <li><a href="#">Home</a></li>
- <li><a href="#">Tutorials</a></li>
- <li><a href="#">Project</a></li>
- <li><a href="#">Blog</a></li>
- <li><a href="#">Contact</a></li>
- </ul>
- </div>
- <input type="button" class="btn btn-primary" id="show_menu" value="Show Menu">
- <input type="button" class="btn btn-primary" id="hide_menu" value="Hide Menu">
- </body>
- </html>
.toggle() Method
jQuery toggle method is used to display elements if hidden and hide elements if visible.
toggle method is useful when you need to show hide elements on single event.
Syntax$(selector).toggle(speed,callback)
An example of toggle menu
- <!DOCTYPE html>
- <html>
- <head>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
- <script>
- $(document).ready(function() {
- $("#show_hide").click(function () {
- $( ".toggle_container" ).toggle({
- duration: 3000,
- });
- });
- });
- </script>
- </head>
- <body>
- <div class="toggle_container">
- <ul>
- <li><a href="#">Home</a></li>
- <li><a href="#">Tutorials</a></li>
- <li><a href="#">Project</a></li>
- <li><a href="#">Blog</a></li>
- <li><a href="#">Contact</a></li>
- </ul>
- </div>
- <input type="button" class="btn btn-primary" id="show_hide" value="Show/Hide Menu">
- </body>
- </html>
.toggleClass() Method
jQuery toggleClass method is used to add remove one or more class names to selected elements.
jQuery toggleClass method first check each elements for class name if class names are missing then it add the class name and if class names are added already then it remove the class name to selected elements.
Syntax$(selector).toggleClass(classname)
An example of toggle menu
- <!DOCTYPE html>
- <html>
- <head>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
- <style>
- .red{ color:red; }
- .green{ color:green; }
- </style>
- <script>
- $(document).ready(function() {
- $("#toggle_classes").click(function () {
- $( ".toggle" ).toggleClass('red green');
- });
- });
- </script>
- </head>
- <body>
- <div class="toggle_container">
- <ul>
- <li class="toggle green">Home</li>
- <li class="toggle green">Tutorials</li>
- <li class="toggle red">Project</li>
- <li class="toggle red">Blog</li>
- <li class="toggle red">Contact</li>
- </ul>
- </div>
- <input type="button" class="btn btn-primary" id="toggle_classes" value="Toggle Classes">
- </body>
- </html>