Adding a slide effect to bootstrap dropdown Adding a slide effect to bootstrap dropdown javascript javascript

Adding a slide effect to bootstrap dropdown


If you update to Bootstrap 3 (BS3), they've exposed a lot of Javascript events that are nice to tie your desired functionality into. In BS3, this code will give all of your dropdown menus the animation effect you are looking for:

  // Add slideDown animation to Bootstrap dropdown when expanding.  $('.dropdown').on('show.bs.dropdown', function() {    $(this).find('.dropdown-menu').first().stop(true, true).slideDown();  });  // Add slideUp animation to Bootstrap dropdown when collapsing.  $('.dropdown').on('hide.bs.dropdown', function() {    $(this).find('.dropdown-menu').first().stop(true, true).slideUp();  });

You can read about BS3 events here and specifically about the dropdown events here.


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:

.dropdown .dropdown-menu {    -webkit-transition: all 0.3s;    -moz-transition: all 0.3s;    -ms-transition: all 0.3s;    -o-transition: all 0.3s;    transition: all 0.3s;    max-height: 0;    display: block;    overflow: hidden;    opacity: 0;}.dropdown.open .dropdown-menu { /* For Bootstrap 4, use .dropdown.show instead of .dropdown.open */    max-height: 300px;    opacity: 1;}

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'm doing something like that but on hover instead of on click.. This is the code I'm using, you might be able to tweak it up a bit to get it to work on click

$('.navbar .dropdown').hover(function() {  $(this).find('.dropdown-menu').first().stop(true, true).delay(250).slideDown();}, function() {  $(this).find('.dropdown-menu').first().stop(true, true).delay(100).slideUp()});