Synchronization of two slideToggle?



  • There's a flashing menu. It's supposed to drive a content. I made an extra block of menuMargin shows when the menu is open. Problems start when I click on another menu reference. введите сюда описание изображения

    http://jsfiddle.net/qz55hhde/2

    jQuery( document ).ready(function( $ ) {
      $(function () {
          var links = $('.menu a.parrent');
          links.click(function (e) {
              links.not(this).siblings('ul').slideUp(300);
              $(this).siblings('ul').slideToggle(300);
                $('.menuMargin').slideToggle(300);
          });
      });
    });
    

    How can we solve this problem?



  • You're trying to put all the logic in a few toggle, and the logic is actually a lot opening from a closed state, switching to another menu, closing the open menu. Just split the whole logic clearly:

    $(function() {
      var links = $('.menu a.parrent');
      var openMenu = null;
      links.click(function() {
        if (openMenu == this) { // закрыть меню
          links.siblings('ul').slideUp(300);
          $('.menuMargin').slideUp(300);
          openMenu = null;
        }
        else if (openMenu != null) { // переключить меню
          links.not(this).siblings('ul').slideUp(300);
          $(this).siblings('ul').slideDown(300);
          $('.menuMargin').slideDown(300);
          openMenu = this;
        }
        else { // открыть меню
          $(this).siblings('ul').slideDown(300);
          $('.menuMargin').slideDown(300);
          openMenu = this;
        }
      });
    });
    

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2