93

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?

j08691
  • 204,283
  • 31
  • 260
  • 272
Ben Beri
  • 1,101
  • 4
  • 23
  • 64
  • 1
    did u include the bootstrap-dropdown.js somewhere? i don't see it in the header.. and awwww these cakes. i'm so hungry now! – ablm Aug 24 '12 at 20:10
  • Hmm it's inside boostrap.js, I did include it but it didnt work so I removed it but i re-added it and yes if I remove the mini one, it wont work at all – Ben Beri Aug 24 '12 at 20:11
  • EDIT: oh yeah, it's included. – ablm Aug 24 '12 at 20:20
  • Umm. The default dropdown doesn't slide; On the modified version (from the links), there is an $el.parent().slideToggle(); which calls a JQuery animation. – ablm Aug 24 '12 at 20:36
  • And btw i dun see why u include bootstrap 2 times. bootstrap.min.js is the minified version. – ablm Aug 24 '12 at 20:43
  • I noticed when you click on the header, it hides and when you click again the menu servers slides back, thats now what I want, I want the drop down slide when it opens, slide back when it closes. – Ben Beri Aug 24 '12 at 21:13
  • hi I have boostrap 4.3.1 and I try add this code in my structure of dropdown because I want to add a a sliding animation how can I add a sliding animation ? – simon Feb 17 '19 at 17:43
  • I see a lot of great answers below, but for those of us who prefer a _zero_ code solution, is using a [Card](https://getbootstrap.com/docs/4.3/components/card/) instead of a dropdown an acceptable UI decision for you? `Cards` can contain control components (e.g. buttons) and play nicely with `Collapse`, which will handle the animation for you out-of-the-box. – ecoe Mar 01 '19 at 19:30

19 Answers19

160

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.

Daniel Dewhurst
  • 2,533
  • 2
  • 21
  • 39
cogell
  • 3,011
  • 1
  • 22
  • 19
  • 4
    I'm having an issue with the slideUp portion of this. While in collapsed mode (Chrome), the width of the dropdown returns to the width as if being viewed in full screen. While using FF, it slides down, but hides instead of sliding up. – ScubaSteve Aug 12 '14 at 18:04
  • 3
    I too see this issue, I actually created another thread asking specifically about it... http://stackoverflow.com/questions/26267207/bootstrap-3-dropdown-slidedown-strange-behavior-when-navbar-collapsed but I don't know exactly how to fix it. I feel like the Dropdown class isn't waiting for the transition to finish before processing the `hidden.bs.dropdown` event. – Kyle Johnson Oct 10 '14 at 01:50
  • 3
    this works and does what i desire, but on mobile viewport, the dropdown slideup for submenus disappears to fast and looks choppy - thanks for the share though! – Uncle Iroh Oct 12 '16 at 12:27
  • I'm unable to get this to work. I keep getting the following error: "Uncaught TypeError: $(...).find(...).first(...).stop is not a function". I don't understand how .stop() is not a function unless the animation has somehow not started yet when the event is fired? What am I missing? – John Jul 06 '18 at 01:18
  • See Slipoch's answer further down for mobile fix. – Tashows Sep 18 '18 at 20:35
  • Its even better if you write some ms inside like this: `.slideDown(350);` – Diogo Carvalho Jan 13 '23 at 22:22
86

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.

Mooseman
  • 18,763
  • 14
  • 70
  • 93
MadisonTrash
  • 5,444
  • 3
  • 22
  • 25
  • 2
    Nice solution, appreciate it. – Lalit Narayan Mishra Feb 19 '16 at 13:20
  • I love this, but unfortunately the slide-down animation only works one time. If I close the menu and open it again without a page refresh, it just opens instantly. Anyone else having this problem? – Nathan R Sep 12 '16 at 23:27
  • 8
    Great solution. Unfortunately, there's a small bug. The first item in the drop-down is still clickable, even when it's collapsed. You can tell by hovering slightly below the collapsed drop-down. Not a big deal in this case, because your first option is plain text. When it's a link, however, it's a problem. My solution is to "animate" the visibility as well, but delay to take place after the other stuff finishes animating. Check it out: https://jsfiddle.net/stamminator/kjLe1tfs/1/ – Jacob Stamm Sep 29 '16 at 21:00
  • Thanks for pointing on that bug, Jacob. Looks like this is problem with padding in list. If you don't mind I'll add your solution to answer. – MadisonTrash Oct 04 '16 at 12:40
  • This accomplishes the animation for me, but when my NavBar is collapsed, I get some weird alignment/spacing issues. Do you know why/how I might be able to fix this? https://codesandbox.io/s/61vjn41mzz – Jed May 10 '19 at 19:43
  • You can use `max-height: 100vh;` if you don't want to set the `max-height` manually. – Laode Muhammad Al Fatih Feb 29 '20 at 05:48
  • 1
    One could also add `pointer-events:none` to the collapsed version, then add `point-events: all` to the menu once it has `.show` – Gray Ayer Jul 23 '20 at 21:16
  • @Jed or anyone else having a weird allignment issue: Adding padding: 0px; in the .dropdown .dropdown-menu block removes the weird extra space in the collapsed menu and all looks great. – Majte Jan 14 '21 at 12:20
22

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()
});
Yohn
  • 2,519
  • 19
  • 23
16

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:

$(function(){
    // ADD SLIDEDOWN ANIMATION TO DROPDOWN //
    $('.dropdown').on('show.bs.dropdown', function(e){
        $(this).find('.dropdown-menu').first().stop(true, true).slideDown();
    });

    // ADD SLIDEUP ANIMATION TO DROPDOWN //
    $('.dropdown').on('hide.bs.dropdown', function(e){
        e.preventDefault();
        $(this).find('.dropdown-menu').first().stop(true, true).slideUp(400, function(){
            //On Complete, we reset all active dropdown classes and attributes
            //This fixes the visual bug associated with the open class being removed too fast
            $('.dropdown').removeClass('show');
            $('.dropdown-menu').removeClass('show');
            $('.dropdown').find('.dropdown-toggle').attr('aria-expanded','false');
        });
    });
});
IndieRok
  • 1,778
  • 1
  • 14
  • 21
  • The demo has one menu getting stuck open after you click a few in a row. – SventoryMang Jun 24 '16 at 04:03
  • @DOTang yeah, I guess it wasn't perfect, hence the quick fix, I guess a little refactoring would make it better. Hopefully by the time Bootstrap4 comes out, they will have fixed it – IndieRok Jun 27 '16 at 12:49
11

here is my solution for slide & fade effect:

   // Add slideup & fadein animation to dropdown
   $('.dropdown').on('show.bs.dropdown', function(e){
      var $dropdown = $(this).find('.dropdown-menu');
      var orig_margin_top = parseInt($dropdown.css('margin-top'));
      $dropdown.css({'margin-top': (orig_margin_top + 10) + 'px', opacity: 0}).animate({'margin-top': orig_margin_top + 'px', opacity: 1}, 300, function(){
         $(this).css({'margin-top':''});
      });
   });
   // Add slidedown & fadeout animation to dropdown
   $('.dropdown').on('hide.bs.dropdown', function(e){
      var $dropdown = $(this).find('.dropdown-menu');
      var orig_margin_top = parseInt($dropdown.css('margin-top'));
      $dropdown.css({'margin-top': orig_margin_top + 'px', opacity: 1, display: 'block'}).animate({'margin-top': (orig_margin_top + 10) + 'px', opacity: 0}, 300, function(){
         $(this).css({'margin-top':'', display:''});
      });
   });
Vedmant
  • 2,265
  • 1
  • 27
  • 36
  • Thanks. Your code works perfectly. However I don't quite understand why we need to have `$(this).css({'margin-top':''});` @Vedmant – DucCuong Mar 06 '15 at 15:57
  • This is to remove margin-top parameter after animation is complete, in case dropdowns have some margins added in custom styles (like in my theme).It have to work without it as it animates to orig_margin_top back, but it's cleaner not to leave extra inline styles after animation is complete. – Vedmant Mar 07 '15 at 20:01
10

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...

.show > .dropdown-menu {
  max-height: 900px;
  visibility: visible;
}

.dropdown-menu {
  display: block;
  max-height: 0;
  visibility: hidden;
  transition: all 0.5s ease-in-out;
  overflow: hidden;
}

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.

Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
8

On click it can be done using below code

$('.dropdown-toggle').click(function() {
  $(this).next('.dropdown-menu').slideToggle(500);
});
m4n0
  • 29,823
  • 27
  • 76
  • 89
7

I am using the code above but I have changed the delay effect by slideToggle.

It slides the dropdown on hover with animation.

$('.navbar .dropdown').hover(function() {
    $(this).find('.dropdown-menu').first().stop(true, true).slideToggle(400);
    }, function() {
    $(this).find('.dropdown-menu').first().stop(true, true).slideToggle(400)
    });
Augusto Triste
  • 461
  • 7
  • 8
  • 1
    don't really need to define it twice. when using a toggle it will work without the second. – Smern May 10 '13 at 23:35
4

For Bootstrap 5 a simple and beautiful slide in animation can be done with a simple keframe animation.

@keyframes slideIn {
    0% {
        transform: translateY(1rem);
        opacity: 0;
    }
    100% {
        transform: translateY(0rem);
        opacity: 1;
    }
}

.slideIn {
    -webkit-animation-name: slideIn;
    animation-name: slideIn;
    animation-duration: 0.4s;
    animation-fill-mode: both;
}
<li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle m-2" href="/" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
            Dropdown
          </a>
          <ul class="dropdown-menu slideIn rounded-2 p-3" aria-labelledby="navbarDropdown">
            <li><a class="dropdown-item" href="/">Action</a></li>
            <li><a class="dropdown-item" href="/">Another action</a></li>
            <li><hr class="dropdown-divider"/></li>
            <li><a class="dropdown-item" href="/">Something else here</a></li>
          </ul>
        </li>
3

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:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.4.0/animate.min.css">

Step 2: Use the standard Bootstrap HTML on the trigger:

<div class="dropdown">
  <button type="button" data-toggle="dropdown">Dropdown trigger</button>

  <ul class="dropdown-menu">
    ...
  </ul>
</div>

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

<ul class="dropdown-menu" data-dropdown-in="fadeIn" data-dropdown-out="fadeOut">
......
</ul>

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):

var dropdownSelectors = $('.dropdown, .dropup');

// Custom function to read dropdown data
// =========================
function dropdownEffectData(target) {
  // @todo - page level global?
  var effectInDefault = null,
      effectOutDefault = null;
  var dropdown = $(target),
      dropdownMenu = $('.dropdown-menu', target);
  var parentUl = dropdown.parents('ul.nav'); 

  // If parent is ul.nav allow global effect settings
  if (parentUl.size() > 0) {
    effectInDefault = parentUl.data('dropdown-in') || null;
    effectOutDefault = parentUl.data('dropdown-out') || null;
  }

  return {
    target:       target,
    dropdown:     dropdown,
    dropdownMenu: dropdownMenu,
    effectIn:     dropdownMenu.data('dropdown-in') || effectInDefault,
    effectOut:    dropdownMenu.data('dropdown-out') || effectOutDefault,  
  };
}

// Custom function to start effect (in or out)
// =========================
function dropdownEffectStart(data, effectToStart) {
  if (effectToStart) {
    data.dropdown.addClass('dropdown-animating');
    data.dropdownMenu.addClass('animated');
    data.dropdownMenu.addClass(effectToStart);    
  }
}

// Custom function to read when animation is over
// =========================
function dropdownEffectEnd(data, callbackFunc) {
  var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
  data.dropdown.one(animationEnd, function() {
    data.dropdown.removeClass('dropdown-animating');
    data.dropdownMenu.removeClass('animated');
    data.dropdownMenu.removeClass(data.effectIn);
    data.dropdownMenu.removeClass(data.effectOut);

    // Custom callback option, used to remove open class in out effect
    if(typeof callbackFunc == 'function'){
      callbackFunc();
    }
  });
}

// Bootstrap API hooks
// =========================
dropdownSelectors.on({
  "show.bs.dropdown": function () {
    // On show, start in effect
    var dropdown = dropdownEffectData(this);
    dropdownEffectStart(dropdown, dropdown.effectIn);
  },
  "shown.bs.dropdown": function () {
    // On shown, remove in effect once complete
    var dropdown = dropdownEffectData(this);
    if (dropdown.effectIn && dropdown.effectOut) {
      dropdownEffectEnd(dropdown, function() {}); 
    }
  },  
  "hide.bs.dropdown":  function(e) {
    // On hide, start out effect
    var dropdown = dropdownEffectData(this);
    if (dropdown.effectOut) {
      e.preventDefault();   
      dropdownEffectStart(dropdown, dropdown.effectOut);   
      dropdownEffectEnd(dropdown, function() {
        dropdown.dropdown.removeClass('open');
      }); 
    }    
  }, 
});

Step 5 (optional): If you want to speed up or alter the animation you can do so with CSS like the following:

.dropdown-menu.animated {
  /* Speed up animations */
  -webkit-animation-duration: 0.55s;
  animation-duration: 0.55s;
  -webkit-animation-timing-function: ease;
  animation-timing-function: ease;
}

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

Tom James
  • 184
  • 1
  • 3
  • 3
    It would be good to at least include some details in your answer - just linking to your site isn't really a good answer. – ajshort Jan 04 '16 at 10:10
  • @GeraldSchneider updated the answer with more detail. Was my first answer on stackoverflow so appreciate the feedback. – Tom James Jan 26 '16 at 18:41
3

Intro

As of the time of writing, the original answer is now 8 years old. Still I feel like there isn't yet a proper solution to the original question.

Bootstrap has gone a long way since then and is now at 4.5.2. This answer addresses this very version.

The problem with all other solutions so far

The issue with all the other answers is, that while they hook into show.bs.dropdown / hide.bs.dropdown, the follow-up events shown.bs.dropdown / hidden.bs.dropdown are either fired too early (animation still ongoing) or they don't fire at all because they were suppressed (e.preventDefault()).

A clean solution

Since the implementation of show() and hide() in Bootstraps Dropdown class share some similarities, I've grouped them together in toggleDropdownWithAnimation() when mimicing the original behaviour and added little QoL helper functions to showDropdownWithAnimation() and hideDropdownWithAnimation().
toggleDropdownWithAnimation() creates a shown.bs.dropdown / hidden.bs.dropdown event the same way Bootstrap does it. This event is then fired after the animation completed - just like you would expect.

/**
 * Toggle visibility of a dropdown with slideDown / slideUp animation.
 * @param {JQuery} $containerElement The outer dropdown container. This is the element with the .dropdown class.
 * @param {boolean} show Show (true) or hide (false) the dropdown menu.
 * @param {number} duration Duration of the animation in milliseconds
 */
function toggleDropdownWithAnimation($containerElement, show, duration = 300): void {
    // get the element that triggered the initial event
    const $toggleElement = $containerElement.find('.dropdown-toggle');
    // get the associated menu
    const $dropdownMenu = $containerElement.find('.dropdown-menu');
    // build jquery event for when the element has been completely shown
    const eventArgs = {relatedTarget: $toggleElement};
    const eventType = show ? 'shown' : 'hidden';
    const eventName = `${eventType}.bs.dropdown`;
    const jQueryEvent = $.Event(eventName, eventArgs);

    if (show) {
        // mimic bootstraps element manipulation
        $containerElement.addClass('show');
        $dropdownMenu.addClass('show');
        $toggleElement.attr('aria-expanded', 'true');
        // put focus on initial trigger element
        $toggleElement.trigger('focus');
        // start intended animation
        $dropdownMenu
            .stop() // stop any ongoing animation
            .hide() // hide element to fix initial state of element for slide down animation
            .slideDown(duration, () => {
            // fire 'shown' event
            $($toggleElement).trigger(jQueryEvent);
        });
    }
    else {
        // mimic bootstraps element manipulation
        $containerElement.removeClass('show');
        $dropdownMenu.removeClass('show');
        $toggleElement.attr('aria-expanded', 'false');
        // start intended animation
        $dropdownMenu
            .stop() // stop any ongoing animation
            .show() // show element to fix initial state of element for slide up animation
            .slideUp(duration, () => {

            // fire 'hidden' event
            $($toggleElement).trigger(jQueryEvent);
        });
    }
}

/**
 * Show a dropdown with slideDown animation.
 * @param {JQuery} $containerElement The outer dropdown container. This is the element with the .dropdown class.
 * @param {number} duration Duration of the animation in milliseconds
 */
function showDropdownWithAnimation($containerElement, duration = 300) {
    toggleDropdownWithAnimation($containerElement, true, duration);
}

/**
 * Hide a dropdown with a slideUp animation.
 * @param {JQuery} $containerElement The outer dropdown container. This is the element with the .dropdown class.
 * @param {number} duration Duration of the animation in milliseconds
 */
function hideDropdownWithAnimation($containerElement, duration = 300) {
    toggleDropdownWithAnimation($containerElement, false, duration);
}

Bind Event Listeners

Now that we have written proper callbacks for showing / hiding a dropdown with an animation, let's actually bind these to the correct events.

A common mistake I've seen a lot in other answers is binding event listeners to elements directly. While this works fine for DOM elements present at the time the event listener is registered, it does not bind to elements added later on.

That's why you are generally better off binding directly to the document:

$(function () {

    /* Hook into the show event of a bootstrap dropdown */
    $(document).on('show.bs.dropdown', '.dropdown', function (e) {
        // prevent bootstrap from executing their event listener
        e.preventDefault();
        
        showDropdownWithAnimation($(this));
    });

    /* Hook into the hide event of a bootstrap dropdown */
    $(document).on('hide.bs.dropdown', '.dropdown', function (e) {
        // prevent bootstrap from executing their event listener
        e.preventDefault();
          
        hideDropdownWithAnimation($(this));
    });

});
Tobias Wicker
  • 617
  • 8
  • 12
2
$('.navbar .dropdown').hover(function() {
    $(this).find('.dropdown-menu').first().stop(true, true).slideDown();
}, function() {
    $(this).find('.dropdown-menu').first().stop(true, true).slideUp();
});

This code works if you want to reveal dropdowns on hover.

I just changed the .slideToggle to .slideDown & .slideUp, and removed the (400) timing

2

Here is a nice simple solution using jQuery that works nicely:

$('.dropdown-toggle').click(function () {
    $(this).next('.dropdown-menu').slideToggle(300);
});

$('.dropdown-toggle').focusout(function () {
    $(this).next('.dropdown-menu').slideUp(300);
})

The slide animation toggle occurs on clicking and it always slides back up on losing focus.

Alter the 300 value to anything you want, the lower the number the faster the animation.

Edit:

This solution will only work for desktop views. It will need some further modification in order to display nicely for mobile.

Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
  • This does work for me, but after I move the mouse onto the submenu (ie away from `dropdown-toggle`) it disappears again, meaning I can't select the sub menu items – Bassie May 26 '17 at 23:47
2

I recommend using transform instead of max-height, it is faster, and GPU accelerated.

For Bootstrap 5 add the following CSS:

 .dropdown .dropdown-menu {
    -webkit-transition: all 0.32s;
    -moz-transition: all 0.32s;
    -ms-transition: all 0.32s;
    -o-transition: all 0.32s;
    transition: all 0.32s;

    display: block;
    overflow: hidden;
    opacity: 0;
    transform: translateX(-25%) scaleY(0);
    transform-origin: top;
  }

  .dropdown-menu.show {
    opacity: 1;
    transform: translateX(-25%) scaleY(1);
  }
Fayez Nazzal
  • 319
  • 2
  • 10
1

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();

// ADD SLIDEUP ANIMATION TO DROPDOWN //
$('.dropdown').on('hide.bs.dropdown', function(e){
  e.preventDefault();
  $(this).find('.dropdown-menu').first().stop(true, true).slideUp(300, function(){
    $(this).parent().removeClass('open');
  });
});
Slipoch
  • 750
  • 10
  • 23
1

For Bootstrap 3, this variation on the answers above makes the mobile slideUp() animation smoother; the answers above have choppy animation because Bootstrap removes the .open class from the toggle's parent immediately, so this code restores the class until the slideUp() animation is finished.

// Add animations to topnav dropdowns
// based on https://stackoverflow.com/a/19339162
// and https://stackoverflow.com/a/52231970

$('.dropdown')
  .on('show.bs.dropdown', function() {
    $(this).find('.dropdown-menu').first().stop(true, true).slideDown(300);
  })
  .on('hide.bs.dropdown', function() {
    $(this).find('.dropdown-menu').first().stop(true, false).slideUp(300, function() {
      $(this).parent().removeClass('open');
    });
  })
  .on('hidden.bs.dropdown', function() {
    $(this).addClass('open');
  });

Key differences:

  • In the hide.bs.dropdown event handler I'm using .stop()'s default value (false) for its second argument (jumpToEnd)
  • The hidden.bs.dropdown event handler restores the .open class to the dropdown toggle's parent, and it does this pretty much immediately after the class has been first removed. Meanwhile the slideUp() animation is still running, and just like in the answers above, its "the-animation-is-completed" callback is responsible for finally removing the .open class from its parent.
  • Methods are chained together because the selector for each event handler is the same
Jordan Bradford
  • 337
  • 2
  • 8
0
 $(this).find('.dropdown-menu').first().stop(true, true).slideUp(300, function(){
    $(this).parent().removeClass('open');
  });

i have just replace .slideUp with .slideToggle and its working fine for up and down.

0

This simply worked fine for me.

 .dropdown-menu {
    transition: all linear .5s;
}

.dropdown-menu.show {
    display: block;
    transition: all linear .5s;
}
  • 1
    Hi welcome to SO! This is a question dating from 2012, with an accepted answer in 2016. The transition CSS attribute was just introduced in 2012. Regardless, your answer does not really contain anything of value in this case. – geertjanknapen Jul 25 '22 at 14:22
0

It works in Bootstrap 5 and React-Bootstrap...

.dropdown-menu {
        display: block;
        transform-origin: top;
        transform: scaleY(0);
        opacity: 0;
        overflow: hidden;
        transition: all 0.3s ease-in-out;
        -webkit-transition: all 0.3s ease-in-out;
    }
    
    .dropdown.show .dropdown-menu { 
        opacity: 1;
        transform: scaleY(1);
        overflow: visible;
    }