I'm trying to create a simple Vue component that swaps out two elements by sliding one left and simultaneously sliding in the second from the right to replace it. And then vice-versa when triggered again.
I can get the content to slide out ok, but it doesn't seem to slide in, it just appears.
Also during the transition both the elements are visible which expands the parent element, and I am struggling to get them to slide side-by-side.
I've tried using absolute positioning for the elements but the parent element obviously loses it's height and I don't want to hard-code a height value if possible.
<script setup>
import { ref } from 'vue';
const isActivated = ref(false)
</script>
<template>
<div class="p-2 relative overflow-hidden">
<Transition name="slide-left">
<div v-show="!isActivated" class="w-full">
<slot name="first-content" />
</div>
</Transition>
<Transition name="slide-right">
<div v-show="isActivated" class="w-full absolute top-0 left-0">
<slot name="second-content" />
</div>
</Transition>
<div @click.prevent.stop="isActivated = !isActivated" class="px-2 absolute top-0 right-2" >
<slot name="trigger" />
</div>
</div>
</template>
<style>
.slide-left-enter-active,
.slide-left-leave-active,
.slide-right-enter-active,
.slide-right-leave-active{
transition: all .8s;
}
.slide-left-enter-from,
.slide-left-leave-to{
transform: translateX(-100%);
opacity:0;
}
.slide-left-enter-to,
.slide-left-leave-from{
transform: translateX(0%);
opacity:100
}
.slide-right-enter-from,
.slide-right-leave-to {
transform: translateX(100%);
opacity:0;
}
.slide-right-enter-to,
.slide-right-leave-from {
transform: translateX(0%);
opacity:100;
}
</style>
I've tried to replicate in a pen too: https://codepen.io/Ben-Wrigley/pen/xxBmPrd
You are using Vue 3 transition class names, in Vue 2, the
fromtransition classes don't have a-fromsuffix (see documentation). So it has to be.slide-left-enterinstead of.slide-left-enter-from, and similarly forslide-right. Fix the classes to make enter transitions work.An easy fix for the stack effect during transition is to position one of elements absolute during transition:
Here it is in a snippet: