31

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");
        }
    );
});
halfer
  • 19,824
  • 17
  • 99
  • 186
MM PP
  • 4,050
  • 9
  • 35
  • 61
  • If my question is bad, please help me to improve it – MM PP Jul 14 '13 at 11:33
  • 1
    i think the -1 is for not putting what youve tried here.. – krishwader Jul 14 '13 at 11:35
  • 3
    I didn't downvote, but whoever did is probably wondering [what you've tried](http://mattgemmell.com/2008/12/08/what-have-you-tried/). Also, if you want clickable elements I'd suggest using anchor tags (within your div and lis) so that your page will work for keyboard users who can't or don't use a mouse or other pointing device. – nnnnnn Jul 14 '13 at 11:35
  • FWIW, that structure is invalid. `li` elements cannot be direct children of `div` elements, only of [`menu`, `ul`, or `ol` elements](http://www.w3.org/TR/html5/grouping-content.html#the-li-element). – T.J. Crowder Jul 14 '13 at 11:36
  • So is the problem with the JS you've shown that it does show and hide the menu, but does it up and down rather than left and right? Or that it doesn't work at all? That [`.toggle()` method](http://api.jquery.com/toggle-event/) was _removed_ from jQuery in version 1.9. – nnnnnn Jul 14 '13 at 11:41
  • it doesn't work at all – MM PP Jul 14 '13 at 11:42
  • 1
    Which version of jQuery are you using? – nnnnnn Jul 14 '13 at 11:42
  • JQuery version: 2.0.3 – MM PP Jul 14 '13 at 11:42
  • .toggle() is deprecated – MM PP Jul 14 '13 at 11:44
  • is there an alternative? – MM PP Jul 14 '13 at 11:45
  • 1
    It _was_ deprecated, but it is now _removed._ You can see your code works with a pre-1.9 version of jQuery: http://jsfiddle.net/APA2S/ – nnnnnn Jul 14 '13 at 11:45
  • Thanks, but i need 2.0.3. Is there any option to do this with 2.03? – MM PP Jul 14 '13 at 11:47

6 Answers6

63

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.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • Thanks, but as I said in my question, I want to use slide left. – MM PP Jul 14 '13 at 11:49
  • 1
    Yes, I was just adding that part. – nnnnnn Jul 14 '13 at 11:49
  • 2
    @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 Jul 14 '13 at 11:51
  • 3
    @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. – nnnnnn Jul 14 '13 at 11:56
13

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>
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

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

  });
});
balexandre
  • 73,608
  • 45
  • 233
  • 342
0
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $(".click-header").click(function(){
            $(this).next(".hidden-content").slideToggle("slow");
            $(this).toggleClass("expanded-header");
        });
    });
</script>
.demo-container {
    margin:0 auto;
    width: 600px;
    text-align:center;
}
.click-header {
    padding: 5px 10px 5px 60px;
    background: url(images/arrow-down.png) no-repeat 50% 50%;
}
.expanded-header {
    padding: 5px 10px 5px 60px;
    background: url(images/arrow-up.png) no-repeat 50% 50%;
}
.hidden-content {
    display:none;
    border: 1px solid #d7dbd8;
    padding: 20px;
    text-align: center;
}
<div class="demo-container">
    <div class="click-header">&nbsp;</div>
    <div class="hidden-content">Lorem Ipsum.</div>
</div>
inMILD
  • 23
  • 5
Porta Shqipe
  • 766
  • 7
  • 8
0

Try this:

<script type="text/javascript">
$.fn.toggleFuncs = function() {
    var functions = Array.prototype.slice.call(arguments),
    _this = this.click(function(){
        var i = _this.data('func_count') || 0;
        functions[i%functions.length]();
        _this.data('func_count', i+1);
    });
}
$('$showmenu').toggleFuncs(
        function() {
           $( ".menu" ).toggle( "drop" );
            },
            function() {
                $( ".menu" ).toggle( "drop" );
            }
); 

</script>

First fuction is an alternative to JQuery deprecated toggle :) . Works good with JQuery 2.0.3 and JQuery UI 1.10.3

John Doe
  • 1
  • 4
0

Use slideToggle(500) function with a duration in milliseconds for getting a better effect.

Sample Html

<body>
    <div class="growth-step js--growth-step">
        <div class="step-title">
            <div class="num">2.</div>
            <h3>How Can Aria Help Your Business</h3>
        </div>
        <div class="step-details ">
            <p>At Aria solutions, we’ve taken the consultancy concept one step further by offering a full service
                management organization with expertise. </p>
        </div>
    </div>
    <div class="growth-step js--growth-step">
        <div class="step-title">
            <div class="num">3.</div>
            <h3>How Can Aria Help Your Business</h3>
        </div>
        <div class="step-details">
            <p>At Aria solutions, we’ve taken the consultancy concept one step further by offering a full service
                management organization with expertise. </p>
        </div>
    </div>
</body>

In your js file, if you need child propagation for the animation then remove the second click event function and its codes.

$(document).ready(function(){
    $(".js--growth-step").click(function(event){
       $(this).children(".step-details").slideToggle(500);
         return false;
    });
//for stoping child to manipulate the animation
    $(".js--growth-step .step-details").click(function(event) {
        event.stopPropagation();
   });
});
Rafiq
  • 8,987
  • 4
  • 35
  • 35