Search code examples
cssvue.jsvuejs2transition

How to add a smooth Vue collapse transition on v-if


I'm struggling with Vue transitions trying to show/hide content using v-if smoothly. Whilst I understand the CSS classes and transitions, I can make the content appear 'smoothly' using things like opacity or translation, etc... but once the animation is complete (or rather as it starts), any HTML sections below seem to 'jump'.

I'm trying to achieve the same effect as the Bootstrap 4 'collapse' class - click one of the top buttons here: https://getbootstrap.com/docs/4.0/components/collapse/

As the hidden section appears/disappears, all the HTML content 'slides' nicely with it.

Is it possible to use Vue transition for content being shown using v-if? All the samples on the Vue transitions docs, whilst having great CSS transition effects, have the below HTML 'jump' once the transition has started or is complete.

I've seen some pure JS solutions using max-height - https://jsfiddle.net/wideboy32/7ap15qq0/134/

and tried with Vue: https://jsfiddle.net/wideboy32/eywraw8t/303737/

.smooth-enter-active, .smooth-leave-active {
  transition: max-height .5s;
}
.smooth-enter, .smooth-leave-to {
  max-height: 0 .5s;
}

Solution

  • If you want to animate max-height, then you should enter the amount of max-height for the element you want to animate, also correct the second class as you put 's' (or seconds) in max-height definition:

    p{
      max-height: 20px;
    }
    .smooth-enter-active, .smooth-leave-active {
      transition: max-height .5s;
    }
    .smooth-enter, .smooth-leave-to {
      max-height: 0;
    }
    

    if you want something like bs4 collapse then the example inside vue website will do :

    .smooth-enter-active, .smooth-leave-active {
      transition: opacity .5s;
    }
    .smooth-enter, .smooth-leave-to {
      opacity: 0
    }
    

    What you are trying to do is achievable by first finding out the height of the content and then setting it inside .*-enter-to and .*-leave classes. One way to do that is demonstrated in JSFiddle below:

    https://jsfiddle.net/rezaxdi/sxgyj1f4/3/

    You can also completely forget about v-if or v-show and just hide the element using height value which I think is a lot smoother:

    https://jsfiddle.net/rezaxdi/tgfabw65/9/