Search code examples
javascriptjquerycsstwitter-bootstrapnavigation

Bootstrap 4 collapse has delay


I'm creating offcanvas navigation menu on mobile view and I have encountered a problem where there is like 0.3s delay before adding class .show.

I tried adding custom class .active via jquery but results are the same - even tho that class is added without delay, transition on this class is still delayed.

.collapse.active{
  transform: translateX(0%);
}

That's most likely caused by adding and calculating inline style height that I don't really need as i alyways want nav to be 100vh. Is there a way to cut or speed up the calculating part?

Tried adding this code but doesn't help at all.

.collapsing {
-webkit-transition: all 0s ease-out;
-o-transition: all 0s ease-out;
transition: all 0s ease-out;
height:0 !important;
display: none;
}

Bootply link (with delay): https://www.bootply.com/9dFOT7Q2Ct

I did same thing on codepen and there it works just fine - It doesnt even add inline style to #navbarNav so maybe a jquery issue (I'm using recommended jquery-3.2.1)

Codepen link (without delay for some reason): https://codepen.io/janheder/pen/rGLJLb


Solution

  • You might be experiencing a delay because bootstrap adds a collapsing class before show to the navbar to offer more control over animation.

    If you are looking for a smooth transition I believe this might help: https://www.bootply.com/rPqvbgcCVj

    body {
      overflow-x: hidden;
      -webkit-transition: margin .3s ease;
      -moz-transition: margin .3s ease;
      -o-transition: margin .3s ease;
      transition: margin .3s ease;
      width: 100%;
      margin: 0;
      font-family: -apple-system,
        BlinkMacSystemFont,
        "Segoe UI",
        Roboto,
        "Helvetica Neue",
        Arial,
        sans-serif;
      font-size: 1rem;
      font-weight: 400;
      line-height: 1.5;
      color: #212529;
      background-color: #fff;
    }
    
    .navbar-light .navbar-nav .nav-link {
      padding-right: 1.5rem;
      padding-left: 1.5rem;
      position: relative;
    }
    
    .navbar .menu-item.menu-item-has-children > .nav-link:after {
      content: "";
      width: 0;
      height: 0;
      border-style: solid;
      position: absolute;
      right: 0;
      top: 50%;
      -ms-transform: translateY(-50%);
      -webkit-transform: translateY(-50%);
      transform: translateY(-50%);
      border-width: 4px 4px 0px 4px;
      border-color: #007bff transparent transparent transparent;
    }
    /* Added the following code and removed previous code */
    .collapsing {
      transform: translateX(0%);
    }
    
    @media (max-width: 991px) {
      body.active {
        overflow: hidden;
        height: 100%;
        width: 100%;
        margin-left: -280px;
      }
      .collapse.active {
        transform: translateX(0%);
      }
    
      .navbar-collapse {
        position: fixed;
        right: 0px;
        top: 0px;
        display: block;
        transform: translateX(100%);
        height: 100vh;
        width: 280px;
        transition: transform .3s ease;
        overflow-y: scroll;
        background: #fff;
      }
      /* Added the following code */
      .active.collapsing {
        transform: translateX(0%);
      }
    
    
      .navbar .menu-item.menu-item-has-children > .nav-link:after {
        right: 1.5rem;
      }
    }