JQuery show and hide div on mouse click (animate)

317,356

Solution 1

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.

Solution 2

Of course slideDown and slideUp don't do what you want, you said you want it to be left/right, not top/down.

If your edit to your question adding the jquery-ui tag means you're using jQuery UI, I'd go with nnnnnn's solution, using jQuery UI's slide effect.

If not:

Assuming the menu starts out visible (edit: oops, I see that isn't a valid assumption; see note below), if you want it to slide out to the left and then later slide back in from the left, you could do this: Live Example | Live Source

$(document).ready(function() {
    // Hide menu once we know its width
    $('#showmenu').click(function() {
        var $menu = $('.menu');
        if ($menu.is(':visible')) {
            // Slide away
            $menu.animate({left: -($menu.outerWidth() + 10)}, function() {
                $menu.hide();
            });
        }
        else {
            // Slide in
            $menu.show().animate({left: 0});
        }
    });
});

You'll need to put position: relative on the menu element.

Note that I replaced your toggle with click, because that form of toggle was removed from jQuery.


If you want the menu to start out hidden, you can adjust the above. You want to know the element's width, basically, when putting it off-page.

This version doesn't care whether the menu is initially-visible or not: Live Copy | Live Source

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<div id="showmenu">Click Here</div>
<div class="menu" style="display: none; position: relative;"><ul><li>Button1</li><li>Button2</li><li>Button3</li></ul></div>
  <script>
    $(document).ready(function() {
        var first = true;

        // Hide menu once we know its width
        $('#showmenu').click(function() {
            var $menu = $('.menu');
            if ($menu.is(':visible')) {
                // Slide away
                $menu.animate({left: -($menu.outerWidth() + 10)}, function() {
                    $menu.hide();
                });
            }
            else {
                // Slide in
                $menu.show().css("left", -($menu.outerWidth() + 10)).animate({left: 0});
            }
        });
    });
  </script>
</body>
</html>

Solution 3

I would do something like this

DEMO in JsBin: http://jsbin.com/ofiqur/1/

  <a href="#" id="showmenu">Click Here</a>
  <div class="menu">
    <ul>
      <li><a href="#">Button 1</a></li>
      <li><a href="#">Button 2</a></li>
      <li><a href="#">Button 3</a></li>
    </ul>
  </div>

and in jQuery as simple as

var min = "-100px", // remember to set in css the same value
    max = "0px";

$(function() {
  $("#showmenu").click(function() {

    if($(".menu").css("marginLeft") == min) // is it left?
      $(".menu").animate({ marginLeft: max }); // move right
    else
      $(".menu").animate({ marginLeft: min }); // move left

  });
});
Share:
317,356
MM PP
Author by

MM PP

Updated on July 05, 2020

Comments

  • MM PP
    MM PP almost 4 years

    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");
            }
        );
    });
    
  • MM PP
    MM PP almost 11 years
    Thanks, but as I said in my question, I want to use slide left.
  • nnnnnn
    nnnnnn almost 11 years
    Yes, I was just adding that part.
  • nnnnnn
    nnnnnn almost 11 years
    @balexandre - Yes I know, but I started out explaining why the OP's code didn't work as is and how to get it to work - he or she did have the .slideUp() and .slideDown() methods in their code. Then I edited in a left-to-right thing, and you downvoted after that, though perhaps the answer hadn't refreshed in your browser?
  • nnnnnn
    nnnnnn almost 11 years
    @balexandre Why? Because the OP tagged their question with "jquery-ui", and if they want to use it they can implement their requirement with one line of code. If they don't want to use jQuery-UI then T.J.'s answer is a good solution, as is yours.