javascript - Menu Open & Close on menu button - Jquery - Stack Overflow

I'm trying to create a slide out menu, that open an closes on the same a tag. I've put someth

I'm trying to create a slide out menu, that open an closes on the same a tag. I've put something together but, it runs through the whole animation instead of pausing after opening.

HTML

<header>
    <nav>
        <ul class="slide-menu">
            <li class="menu-element"><a href="#" id="aboutopen">How tall?</a></li>
            <li class="menu-element"><a href="#book">Books</a></li>
            <li class="menu-element"><a href="weblink" target="_blank">Journal</a></li>
            <li class="menu-element"><a href="#" id="aboutcontact">Contact</a></li>
        </ul>
        <a id="puller" href="#">Menu</a>
    </nav>
</header>

Jquery

$(document).ready(function()
  {
   $("#puller").click(function(){
   $(".slide-menu").animate({
   marginLeft: '+=360px'
}, 500);
 });

 $("#puller").click(function(){
   $(".slide-menu").animate({
   marginLeft: '-=360px'
 }, 500);
 });


});

Can anyone help with this?

I'm trying to create a slide out menu, that open an closes on the same a tag. I've put something together but, it runs through the whole animation instead of pausing after opening.

HTML

<header>
    <nav>
        <ul class="slide-menu">
            <li class="menu-element"><a href="#" id="aboutopen">How tall?</a></li>
            <li class="menu-element"><a href="#book">Books</a></li>
            <li class="menu-element"><a href="weblink" target="_blank">Journal</a></li>
            <li class="menu-element"><a href="#" id="aboutcontact">Contact</a></li>
        </ul>
        <a id="puller" href="#">Menu</a>
    </nav>
</header>

Jquery

$(document).ready(function()
  {
   $("#puller").click(function(){
   $(".slide-menu").animate({
   marginLeft: '+=360px'
}, 500);
 });

 $("#puller").click(function(){
   $(".slide-menu").animate({
   marginLeft: '-=360px'
 }, 500);
 });


});

Can anyone help with this?

Share Improve this question asked Nov 13, 2013 at 22:39 user2674488user2674488 11 silver badge1 bronze badge 1
  • Left an answer, if it works, please "accept" it. :) – Mike Barwick Commented Nov 13, 2013 at 22:57
Add a ment  | 

2 Answers 2

Reset to default 3

Using jQuery toggle is a good idea. You can also simply maintain the state of the menu as to slided out already or not and take action like this

$(document).ready(function(){

   var slide = false;
   $("#puller").click(function(){

   if(slide){
        $(".slide-menu").animate({marginLeft: '-=360x'}, 500);
        slide = false;
   }else{
       $(".slide-menu").animate({marginLeft: '+=360px'}, 500);
        slide = true
   }
   });

});

Use jQuery Toggle, like so. Simple.

$(document).ready(function() {
    var menu = $('.slide-menu');
    var speed = 500; // set animation speed

    $('#puller').toggle(
        function(){
            menu.animate({
                marginLeft: '+=360px'
            }, speed);
        },
        function(){
            menu.animate({
                marginLeft: '-=360px'
            }, speed);
        }
    );
)};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745256486a4618988.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信