Nesta
Nesta

Reputation: 1008

Click the same item to close and open dropdown only when the previous is closed

I need to add 2 extra functionalities to this nav I'm building:

1 - When I click on an active nav item the drop-down should close and the .black-bg class should be removed from the main-container as well as the .active class in the nav item. At the moment this only works when I click outside the drop-down.

2 - When a dropdown is open and you click to open a second one, the second one will have to wait until the first one closes. At the moment both animations happen at the same time.

See Demo

$(document).ready(function() {
  $(".click").on("click", function(evt) {
      evt.stopPropagation();
    if ($(this).hasClass("active")) {
      return;
    }
      $(".active").parent().find(".showup").slideToggle(200);
      $(".active").toggleClass("active");
      $(this).toggleClass("active");
      $(this).parent().find(".showup").slideToggle(200);
      if (!$(".main-container").hasClass("black-bg")) {
        $(".main-container").toggleClass("black-bg");
      }
  });
  $(".showup").on("click", function(evt) {
      evt.stopPropagation();
  });

});

$(document).on("click", function() {
    $(".active").parent().find(".showup").slideUp(50);
    $(".active").toggleClass("active");
    if ($(".main-container").hasClass("black-bg")) {
      $(".main-container").toggleClass("black-bg");
    }
});

I'm really stuck on this. I hope someone could help me. Thank you.

Upvotes: 1

Views: 143

Answers (2)

Takit Isy
Takit Isy

Reputation: 10081

I suggest you do to something like this:
(See the comments in my code)

// Added functions, because it's handy:
// Any global modification is to be made only there!
function menus_close() {
  $(".active").parent().find(".showup").slideUp(200); // 200 to better see it in action
  $(".active").removeClass("active");
  $(".main-container").removeClass("black-bg"); 
}

function menu_open(elm) {
  setTimeout(function() { // Fire the function after some time (see below)
    elm.addClass("active");
    elm.parent().find(".showup").slideDown(200);
    $(".main-container").addClass("black-bg");
  }, 200); // Here is the time
}

$(document).ready(function() {

  $(".click").on("click", function(evt) {
    evt.stopPropagation();
    if ($(this).hasClass("active")) {
      menus_close();
      return;
    }else{
      menus_close();
      menu_open($(this));
    }
  });

  $(".showup").on("click", function(evt) {
    evt.stopPropagation();
  });

});

$(document).on("click", function() {
  menus_close();
});
* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

.nav-wrapper {
  width: 100%;
  overflow: hidden;
  background: #424242;
}

nav {
  width: 100%;
  /* modified to work in snippet */
  margin: auto;
  overflow: hidden;
  background: #424242;
}

.nav-content {
  width: 100%;
  z-index: 999;
  background: #ccc;
}

.top-bar-section {
  float: right;
}

.top-bar-section a.active {
  background: #f00;
}

.showup {
  display: none;
  background: #ccc;
  position: absolute;
  width: 100%;
  top: 70px;
  left: 0;
  z-index: 99;
  padding: 30px 15px 30px 20px;
}

p {
  font-size: 14px;
  line-height: 1.4;
}

li.nav-item {
  display: inline-block;
  background: #f5f5f5;
}

li.nav-item a {
  display: block;
  text-decoration: none;
  padding: 10px;
}

.main-container {
  width: 80%;
  height: 400px;
  margin: auto;
}

.black-bg {
  background: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="nav-wrapper">
  <nav>
    <div class="top-bar-section">
      <ul>
        <li class="nav-item">
          <a href="#" class="click">Nav item 1</a>
          <div class="showup">
            <div class="nav-content">
              <p>
                Dropdown for Nav Item 1.
              </p>
            </div>
          </div>
        </li>
        <li class="nav-item">
          <a href="#" class="click">Nav item 2</a>
          <div class="showup">
            <div class="nav-content">
              <p>
                Dropdown for Nav Item 2.
              </p>
            </div>
          </div>
        </li>
        <li class="nav-item">
          <a href="#" class="click">Nav item 3</a>
        </li>
        <li class="nav-item">
          <a href="#" class="click">Nav item 4</a>
        </li>
      </ul>
    </div>
  </nav>
</div>
<div class="main-container">

</div>

I hope it helps.

Upvotes: 1

Jithin Raj  P R
Jithin Raj P R

Reputation: 6797

This will work for you fine.

The below code is the change added by me.

$(".click").on("click", function(evt) {
  evt.stopPropagation();
  var th = $(this);

  if (th.hasClass("active")) {
    th.removeClass("active");
    th.parent().find(".showup").slideUp(200);
    $(".main-container").removeClass("black-bg");
  } else {
    $(".active").removeClass("active");
    $(".showup").slideUp(200);
    $(".main-container").removeClass("black-bg");
    setTimeout(function() {
      th.addClass("active");
      th.parent().find(".showup").slideDown(200);
      $(".main-container").addClass("black-bg");
    }, 250);

  }

});

the codepen link

Upvotes: 1

Related Questions