简体   繁体   English

如何通过单击关闭菜单

[英]How to close a menu by clicking on it

I have the following code that works fine: 我有下面的代码工作正常:

It is hosted at codepen here: 它托管在codepen此处: http://codepen.io/anon/pen/opfDd http://codepen.io/anon/pen/opfDd

$(document).ready(function(){
  var animTime = 300,
      clickPolice = false;

  $(document).on('touchstart click', '.acc-btn', function(){
    if(!clickPolice){
       clickPolice = true;

      var currIndex = $(this).index('.acc-btn'),
          targetHeight = $('.acc-content-inner').eq(currIndex).outerHeight();

      $('.acc-btn h1').removeClass('selected');
      $(this).find('h1').addClass('selected');

      $('.acc-content').stop().animate({ height: 0 }, animTime);
      $('.acc-content').eq(currIndex).stop().animate({ height: targetHeight }, animTime);

      setTimeout(function(){ clickPolice = false; }, animTime);
    }

  });

});

The thing is that if I click on an already opened menu (its h1), it does not close. 问题是,如果我单击一个已经打开的菜单(它的h1),它不会关闭。

Since I know very little JavaScript, could someone suggest me an approach to closing a menu that is opened by clicking on its header? 由于我对JavaScript的了解很少,有人可以建议我关闭单击菜单标题打开的菜单的方法吗?

Any pointers will be very much appreciated. 任何指针将不胜感激。

Regards 问候

Marc. 渣。

Updated the JavaScript Please refer the Pen 更新了JavaScript,请参阅

$(document).ready(function(){
  var animTime = 300,
    clickPolice = false;
  $(document).on('touchstart click', '.acc-btn', function(){
    if(!clickPolice){
      clickPolice = true;
      var currIndex = $(this).index('.acc-btn'),
        targetHeight = $('.acc-content-inner').eq(currIndex).outerHeight(),
        expanded = $(this).find('h1').hasClass('selected');

      if(expanded) {
        $('.acc-btn h1').removeClass('selected');
        $('.acc-content').eq(currIndex).stop().animate({ height: targetHeight }, animTime);
        $('.acc-content').stop().animate({ height: 0 }, animTime);
      }else {
        $(this).find('h1').addClass('selected');
        $('.acc-content').stop().animate({ height: 0 }, animTime);
        $('.acc-content').eq(currIndex).stop().animate({ height: targetHeight }, animTime);
      }

      setTimeout(function(){ clickPolice = false; }, animTime);
    }
  });
});

If you want, an easier approach to close an opened title would be to check out jQuery's slideToggle(). 如果需要,关闭打开的标题的一种更简单的方法是检出jQuery的slideToggle()。 It seems like this might be something you would be interested in. 似乎您可能会对这感兴趣。

source: http://api.jquery.com/slidetoggle/ 来源: http//api.jquery.com/slidetoggle/

Here is the updated link: 这是更新的链接:

http://codepen.io/anon/pen/jdqnm http://codepen.io/anon/pen/jdqnm

code: 码:

$(document).ready(function(){
  var animTime = 300,
      clickPolice = false;

  $(document).on('touchstart click', '.acc-btn', function(){
    if(!clickPolice){
       clickPolice = true;

      var currIndex = $(this).index('.acc-btn'),
          targetHeight = $('.acc-content-inner').eq(currIndex).outerHeight();

      $('.acc-btn h1').removeClass('selected');
      $(this).find('h1').addClass('selected');

      $('.acc-content').stop().animate({ height: 0 }, animTime);

      if(!$(this).next().hasClass('open')){
      $('.acc-content').eq(currIndex).stop().animate({ height: targetHeight }, animTime);
        $('.acc-content').eq(currIndex).addClass('open')
     //   opened=true;
      }else{
        $('.acc-content').eq(currIndex).stop().animate({ height: 0 }, animTime);
       $('.acc-content').eq(currIndex).removeClass('open')
      }
      setTimeout(function(){ clickPolice = false; }, animTime);
    }

  });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM