I'm using bootstrap, and I'd like to add animation to a dropdown. I want to add an animation to it, slide down and back up when leaving it. How could I do this?
Things I tried:
Changing the Js drop down file like this:
How can I make Bootstrap's navigation dropdown slide smoothly up and down?
Also it's possible to avoid using JavaScript for drop-down effect, and use CSS3 transition, by adding this small piece of code to your style:
The only problem with this way is that you should manually specify max-height. If you set a very big value, your animation will be very quick.
It works like a charm if you know the approximate height of your dropdowns, otherwise you still can use javascript to set a precise max-height value.
Here is small example: DEMO
! There is small bug with padding in this solution, check Jacob Stamm's comment with solution.
I don't know if I can bump this thread, but I figured out a quick fix for the visual bug that happens when the open class is removed too fast. Basically, all there is to it is to add an OnComplete function inside the slideUp event and reset all active classes and attributes. Goes something like this:
Here is the result: Bootply example
Javascript/Jquery:
On click it can be done using below code
Expanded answer, was my first answer so excuse if there wasn’t enough detail before.
For Bootstrap 3.x I personally prefer CSS animations and I've been using animate.css & along with the Bootstrap Dropdown Javascript Hooks. Although it might not have the exactly effect you're after it's a pretty flexible approach.
Step 1: Add animate.css to your page with the head tags:
Step 2: Use the standard Bootstrap HTML on the trigger:
Step 3: Then add 2 custom data attributes to the dropdrop-menu element; data-dropdown-in for the in animation and data-dropdown-out for the out animation. These can be any animate.css effects like fadeIn or fadeOut
Step 4: Next add the following Javascript to read the data-dropdown-in/out data attributes and react to the Bootstrap Javascript API hooks/events (http://getbootstrap.com/javascript/#dropdowns-events):
Step 5 (optional): If you want to speed up or alter the animation you can do so with CSS like the following:
Wrote an article with more detail and a download if anyones interested: article: http://bootbites.com/tutorials/bootstrap-dropdown-effects-animatecss
Hope that’s helpful & this second write up has the level of detail that’s needed Tom
Update 2018 Bootstrap 4
In Boostrap 4, the
.open
class has been replaced with.show
. I wanted to implement this using only CSS transistions without the need for extra JS or jQuery...Demo: https://www.codeply.com/go/3i8LzYVfMF
Note:
max-height
can be set to any large value that's enough to accommodate the dropdown content.BOOTSTRAP 3 REFERENCE
Added because I keep getting caught by the solution in this thread and it stuffs me up every time.
Basically the BS dropdown immediately removes the
.open
class from the parent, so sliding up does not work.Use the same bit as other solutions for slideDown();