With v-if

A Bonus feature of this module is the <GSAPTransition> component that allows you to use GSAP transitions on v-if/v-show elements.

<GSAPTransition>
  <div v-if="someProp">Hello World</div>
</GSAPTransition>

By default, the wrapped content will animate opacity with a (gsap-)default duration of 0.5s.

You can add custom properties to the hidden object to customize the animation. These values will be merged with the default opacity behaviour, so opacity doesnt need to be specified.

<GSAPTransition :hidden="{ x: -32 }">
  <div v-if="someProp">Hello World</div>
</GSAPTransition>

Multiple elements

In addition to the <Transition> component, Vue provides a <TransitionGroup> for transitioning multiple elements. Mostly this is done with v-for to show the appearance of a list of items.

We utilise Vue's default group component by using the group flag on <GSAPTransition>.

<GSAPTransition group>
  <li 
    v-if="someProp" 
    v-for="item in items"
  >
    {{ item.name }}
  </li>
</GSAPTransition>

Visually transitioning multiple elements is best done with a slight stagger.

To enable a stagger, <GSAPTransition> has to have access to the element's index, which is done using :data-index="idx" on the transitioning elements.

There is a default value of 0.1s for the stagger, which can be configured.

<GSAPTransition group :stagger="0.15">
  <li 
    v-if="someProp" 
    v-for="(item, idx) in items"
    :data-index="idx"
  >
    {{ item.name }}
  </li>
</GSAPTransition>

Properties

PropTypeDescription
hiddenGSAPTweenVarsState of the toggled element when hidden
durationnumber, default: 0.5The duration of the animation in seconds
appearboolean, default: falseWether or not to run the enter-animation on initial load
easestring, default: power1.inOutThe ease of the animation. Can be overriden by the ease modifier in hidden
delaynumber, default: 0Initial delay of the transition animation in seconds
groupboolean, default: falseWether or not the transition applies to multiple elements. Most of the times paired with v-if
staggernumber, default: 0.1Delay the appearance of each next element when using group. For this to work you also have to put :data-index="idx" on the element.

Full Example

<GSAPTransition
  :hidden="{ x: -32 }"
  :duration="1"
  :delay="0.75"
  :appear="true"
  ease="bounce.out"
>
  <div v-if="visible">Hello World</div>
</GSAPTransition>

Disabling default opacity animation

If you want to disable the default opacity behaviour, you can set the opacity property in hidden to 1. This way it will animate from 1 to 1 and essentially disable toggling opacity therefore.

Example:

<GSAPTransition :hidden="{ opacity: 1, x: -32 }">
  <div v-if="visible">Hello World</div>
</GSAPTransition>