8

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>
Michael Ruta
  • 345
  • 3
  • 7
  • 13

5 Answers5

8

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

Ghasem
  • 14,455
  • 21
  • 138
  • 171
7

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.

Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
  • 1
    That works great, but why the need for "position-relative" class? It seems to work without it? – Michael Ruta Apr 26 '18 at 17:11
  • Thanks for the example and the idea @Zim, it works but I found I can fix a couple of things there: - The dropdown didn't work properly on the expanded Navbar. - Animation was a bit broken, because the drop down got vertical padding. Here is the fixed example: https://www.codeply.com/go/09IISWbMoa – Mikha Dec 01 '18 at 01:10
2

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;
}
Nico
  • 21
  • 2
1

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;
}
aroabarberan
  • 157
  • 1
  • 2
  • 10
Abhay Srivastav
  • 754
  • 6
  • 16
0

try with this code :)

            .dropdown-menu {
                float: none;
                position: static;
                padding: 0;
                display: block;
                &.show{
                    max-height: 500px;

                }
            }

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