I'm trying to implement exactly this carousel/slide effect using Vue transition-group:
As you can see, they are animating one list item (step) at a time where the previous step goes up and the current one goes up with its content following the timeline.
I'm not sure what I am trying to accomplish is possible using transition-group
, since the whole parent block will animate, not it's children nodes. If that is the case, I would be happy if I could at least animate the parent block.
Another caveat is that, as I am using transition-group
without v-if
, or without a filtered list, all steps are being rendered by default which isn't good.
This is my HTML structure:
<transition-group class="steps-viewport" name="steps" tag="div">
<div v-for="step in currentStep" :key="step.order" class="step-wrapper">
<h3 class="is-size-5 mb-6 has-text-grey-light">
Passo {{ step.order }}
</h3>
<h1 class="is-size-3">{{ step.title }}</h1>
<h2 class="is-size-4 mt-2 has-text-grey">{{ step.headline }}</h2>
<component
class="mt-5"
v-bind:is="step.component"
@status-changed="handleStatusChange($event)"
></component>
</div>
</transition-group>
And this is my CSS:
.component-wrapper {
width: 100%;
.steps-viewport {
height: calc(100vh - 10rem);
overflow: hidden;
display: flex;
flex-direction: column;
.step-wrapper {
flex: 0 0 calc(100vh - 10rem);
display: flex;
justify-content: center;
flex-direction: column;
}
}
}
Last but not least, the script of my component:
import ProductInfo from "./ProductInfo";
export default {
components: {
ProductInfo
},
props: {
defaultActiveStep: {
type: Number,
default: 1
}
},
watch: {
activeStep() {
this.$emit("step-changed", this.activeStep);
}
},
computed: {
currentStep() {
return this.steps.filter(s => s.order === this.activeStep);
}
},
data: () => {
return {
activeStep: 1,
steps: [
{
order: 1,
title: "Title 1?",
headline:
"Headline 1",
component: "product-info"
},
{
order: 2,
title: "Title 2",
headline:
"Headline 2.",
component: "product-info"
},
{
order: 3,
title: "Title 3",
headline:
"Headline 3.",
component: "product-info"
},
{
order: 4,
title: "Title 4!",
headline:
"Headline 4",
component: "product-info"
}
]
};
},
methods: {
handleStatusChange(status) {
const first = this.steps.shift();
this.steps = this.steps.concat(first);
}
}
};
You need to define special classes to target different stages of the transition, in this case
.steps-enter-active
(mid-transition state) and.steps-enter-to
(end-state).For it to occur on page-load, you also need to pass the
appear
attribute.If you want the entire
order
block to transition you can do so like this:If you want each element within to transition, you can do so like this, adding a
transition-delay
:To also transition out, you'll need to use
transition
instead so you can usemode="out-in"
which allows the element to transition out first, before the next one enters.You'll also need to target the children of the transitioning element in your CSS with
.steps-enter-active > *
. Then, just add a.steps-leave-to
class which defines the state to leave to:Lastly, to have everything shift up smoothly once a new element is added, you can wrap the initial fields inside a div, wrap the new element in a transition and reduce the height of the first div by the height of the new element.
You'll also need to transition the height and configure the timings (transition-delay and duration attribute) to match up correctly: