Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropdown menu CSS on click jquery

I have a on click dropdown menu composed by divs with content.

It have a title and when click with jquery menu shows. It works correctly i just want to animate it. (To down and to up).

The principal div have a position relative because i need to show childs under title with position absolute.

$(".item").click(function() {
  if ($(this).hasClass('active')) {
    $(this).removeClass('active');
  } else {
    $(this).addClass('active');
  }
})
.item {
  position: relative;
}

.item .item_group {
  display: none;
}

.item.active .item_group {
  display: block;
  position: absolute;
  width: 100%;
  background: #fff;
  z-index: 100;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="item active">
  <h6>Title</h6>
  <div class="item_group">
    <div class="item_child">
      content
    </div>
    <div class="item_child">
      content
    </div>
    <div class="item_child">
      content
    </div>
  </div>
</div>

Remember : It works correctly, but i want animate to down and to up for cool result

like image 684
Jordi Castillo Avatar asked Aug 31 '26 18:08

Jordi Castillo


1 Answers

Firstly, note for future reference that your if statement to check the visibility of the element then add/remove the class can be replaced with just a single call to toggleClass()

With regard to your requirement, if you simply want to animate the transition of the element when it's displayed or hidden, you could use slideToggle():

$(".item").click(function() {
  $(this).find('.item_group').slideToggle();
})
.item {
  position: relative;
}

.item .item_group {
  display: none;
  position: absolute;
  width: 100%;
  background: #fff;
  z-index: 100;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="item active">
  <h6>Title</h6>
  <div class="item_group">
    <div class="item_child">
      content
    </div>
    <div class="item_child">
      content
    </div>
    <div class="item_child">
      content
    </div>
  </div>
</div>

This can also be achieved in CSS alone using transition:

$(".item").click(function() {
  $(this).toggleClass('active');
})
.item {
  position: relative;
}

.item .item_group {
  height: 0;
  position: absolute;
  width: 100%;
  background: #fff;
  z-index: 100;
  transition: height 0.5s;
  overflow: hidden;
}

.item.active .item_group {
  height: 55px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="item active">
  <h6>Title</h6>
  <div class="item_group">
    <div class="item_child">
      content
    </div>
    <div class="item_child">
      content
    </div>
    <div class="item_child">
      content
    </div>
  </div>
</div>
like image 191
Rory McCrossan Avatar answered Sep 03 '26 07:09

Rory McCrossan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!