Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery show and hide div on mouse click (animate)

This is my HTML code:

<div id="showmenu">Click Here</div> <div class="menu" style="display: none;">     <ul>         <li>Button1</li>         <li>Button2</li>         <li>Button3</li>     </ul> </div> 

And I want to show .menu on click on #showmenu sliding from left to right (with animate). On click again on #showmenu or anywhere in site page, .menu will hide (slide back to left).

I use JQuery 2.0.3

I've tried this, but it doesn't do what I want.

$(document).ready(function() {     $('#showmenu').toggle(         function() {             $('.menu').slideDown("fast");         },         function() {             $('.menu').slideUp("fast");         }     ); }); 
like image 858
MM PP Avatar asked Jul 14 '13 11:07

MM PP


People also ask

How can I show and hide div on mouse click using jQuery?

To show and hide div on mouse click using jQuery, use the toggle() method. On mouse click, the div is visible and on again clicking the div, it hides.

How do I toggle show and hide in jQuery?

jQuery toggle() MethodThe toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden. hide() is run if an element is visible - This creates a toggle effect.

How do I hide a div tag?

To hide an element, set the style display property to “none”. document. getElementById("element").

What is hide () in jQuery?

jQuery hide() Method The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: To show hidden elements, look at the show() method.


1 Answers

That .toggle() method was removed from jQuery in version 1.9. You can do this instead:

$(document).ready(function() {     $('#showmenu').click(function() {             $('.menu').slideToggle("fast");     }); }); 

Demo: http://jsfiddle.net/APA2S/1/

...but as with the code in your question that would slide up or down. To slide left or right you can do the following:

$(document).ready(function() {     $('#showmenu').click(function() {          $('.menu').toggle("slide");     }); }); 

Demo: http://jsfiddle.net/APA2S/2/

Noting that this requires jQuery-UI's slide effect, but you added that tag to your question so I assume that is OK.

like image 154
nnnnnn Avatar answered Oct 02 '22 12:10

nnnnnn