Vue.js: transition-group animation doesn't work

3.6k views Asked by At

I have a slider written as a transition-group component:

<template>
 <div class="carousel-view">
  <transition-group name="carousel-transition" class="carousel" tag="div">
   <div v-for="(slide, index) in slides"
       :key="index"
       class="slide">
    <div>{{ slide.status }}</div>
    <img :src="slide.img" />
   </div>
  </transition-group>
 <div class="carousel-control">
   <button @click="goToPrevious" class="button button__main">Back</button>
   <button @click="goToNext" class="button button__main">Next</button>
  </div>
 </div>
</template>

In my <style> I have this:

<style lang="scss" scoped>
.carousel {
 overflow: hidden;
 display: inline;
 height: 100%;
 width: 900px;
 min-width: 200px;
 text-align: center;
}
.slide {
 display: inline-block;
 position: relative;
 z-index: 10;
}
.slide:first-of-type, .slide:last-of-type {
 opacity: 0.5;
 position: absolute;
}
.slide:first-of-type {
 right: 50%;
 z-index: 5;
}
.slide:first-of-type {
 right: 50%;
 z-index: 5;
}
.slide:last-of-type {
 left: 50%;
 z-index: 5;
}
.carousel-transition-move {
  transition: transform 1s;
}
</style>

I have two functions in my methods to make my slides move:

goToNext() {
  const firstSlide = this.slides.shift();
  this.slides = this.slides.concat(firstSlide);
},
goToPrevious() {
  const lastSlide = this.slides.pop();
  this.slides = [lastSlide].concat(this.slides);
},

All my slides are currently just a hardcoded array in component's data() with three objects containing an image and some status. Right now I cannot animate slides, nor after clicking on buttons calling those functions, not on the setting an interval. What am I doing wrong?

1

There are 1 answers

1
Varit J Patel On BEST ANSWER

Appearntly, The issue was you were using :key="index", whereas vue thows an Tip for this in console.

[Vue tip]: Do not use v-for index as key on children, this is the same as not using keys.

Hence I changed the :key to slide.status and it worked.

Here is the working JsFiddle

Hope this helps!