The vue-slide-up-down library working with pre-mounted elements only.
- Sliding down case: It will not mount the element before animation starts
- Sliding up case: It will not unmount the element after animation ends
It just manipulating with the element's height and hidden attribute.
Now what if I don't what the target element be mounted when it does not displaying?
- Sliding down case: Before animation starts, I want the element be mounted
- Sliding up case: Once animation complete, I want the element be unmounted, not just hidden
If we try
<slide-up-down
v-if="active"
:active="active"
>
Only show this if "active” is true
</slide-up-down>
it will not be the animation because:
- Sliding down case: animation starts before element mounted
- Sliding up case: the element will be unmounted before animation starts
You need a separate variable controlling whether the component is rendered (I named it
isRenderedbelow).And a setter + getter computed (named
renderedbelow) which sets bothactiveandisRenderedto current value, but in different order:true: turnisRenderedon first, then setactivetotruein$nextTick, so the animation is playedfalse: turnactivetofalsefirst, wait for animation to finish and then setisRenderedtofalse.Vue2 demo:
Vue3 demo:
If you're gonna do this multiple times, you might want to extract it as a stand-alone component. Usage example:
Since the change now comes from the
renderedprop, you can move the computed setter code into awatch:In Vue 2:
Vue 3 example. Sandbox here.
<slide-up-down />.