Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate Dropdown Menu Bootstrap 4

The native way a dropdown will shoe in bootstrap (4.0) is not animated. How can I make it "slide" open as the navbar does when collapsed?

It's worth noting that the dropdown is within the navbar. See below codeply; https://www.codeply.com/go/JKj5onR3ug

<nav class="navbar navbar-dark bg-dark">
  <a class="navbar-brand" href="#">Never expand</a>
  <button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#navbarsExample01" aria-controls="navbarsExample01" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="navbar-collapse collapse" id="navbarsExample01" style="">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
        <div class="dropdown-menu" aria-labelledby="dropdown01">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
    </ul>
  </div>
</nav>
like image 668
Michael Ruta Avatar asked Apr 25 '18 12:04

Michael Ruta


4 Answers

Here's how I made a drop down animation with pure css using transform: scale:

SCSS style

.dropdown {
    .dropdown-menu {
        transition: all 0.5s;
        overflow: hidden;
        transform-origin: top center;
        transform: scale(1,0);
        display: block;
    }
    &:hover {
        .dropdown-menu {
            transform: scale(1);
        }
    }
}

Check demo on Codepen

like image 95
Alex Jolig Avatar answered Nov 19 '22 08:11

Alex Jolig


The simplest way would be to use "collapse" to toggle it instead of "dropdown". Then you just need a little CSS to make sure it displays when the collapsing animation is active. Also note that position-relative is set on the dropdown-menu.

.dropdown-menu.collapsing {
    display:block;
}

Try it on Codeply


"Dropdowns are positioned thanks to Popper.js (except when they are contained in a navbar)."

Because dropdowns inside the Navbar are positioned differently here's another example using standard button dropdowns: https://www.codeply.com/go/vJhVEh9Okd


An alternative is to use one of the Bootstrap 3.x dropdown animation techniques.

like image 25
Zim Avatar answered Nov 19 '22 07:11

Zim


You have to override css for dropdown. Use this css.

.dropdown-menu {
    display: block;
}

.navbar-nav .dropdown-menu {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.5s;
}

.navbar-nav .dropdown-menu.show {
    max-height: 500px;
}
like image 1
Abhay Srivastav Avatar answered Nov 19 '22 08:11

Abhay Srivastav


I like this. Simple and good!

.dropdown-menu {
   display: block;
   opacity: 0;
   padding: 0;
   max-height: 0;
   overflow: hidden;
   transition: all .3s ease-in;
}
 .dropdown-menu.show {
   max-height: 500px;
   opacity: 1;
   padding: 0.5rem 0;
}
like image 1
Nico Avatar answered Nov 19 '22 06:11

Nico