I can wrap a v-for
loop in a transition-group
and make all elements appear at the same time like so:
<script setup>
import {onMounted, reactive} from "vue";
const state = reactive({
hover: false,
fadeIn: false,
})
onMounted(() => {
setTimeout(() => {
state.fadeIn = true
}, 2000)
})
</script>
<template>
<transition-group name="fade-1">
<div class="partner-wrap" v-for="(partner, key) in [1,2,3,4,5]" :key="key" v-if="state.fadeIn">
<p>I am fading in!</p>
</div>
</transition-group>
</template>
But how do I make them appear one by one? I've tried this:
<script setup>
import {onMounted, reactive} from "vue";
const state = reactive({
hover: false,
fadeInArray: [false, false, false, false, false]
})
onMounted(() => {
let val = 0;
setInterval(() => {
state.fadeInArray[val] = true
val++
if (val == state.fadeInArray.length) {
clearInterval()
}
}, 2000)
})
</script>
<template>
<transition-group name="fade-1">
<div class="partner-wrap" v-for="(partner, key) in [1,2,3,4,5]" :key="key">
<p v-if="state.fadeInArray[key]">I am fading in!</p>
</div>
</transition-group>
</template>
but the transition doesn't work and the elements pop in without transition.
Here's my transition scss
:
.fade-1 {
&-enter-active, &-leave-active {
transition: all 1s;
}
&-enter-from, &-leave-to {
opacity: 0;
}
}