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"); } ); });
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.
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.
To hide an element, set the style display property to “none”. document. getElementById("element").
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With