I can't achieve a fade-in fade-out with an image displayed in v-for with VueJS. I want to display image by image using next/prev button.
I read this in documentation :
Vue provides a transition wrapper component, allowing you to add entering/leaving transitions for any element or component in the following contexts:
Conditional rendering (using v-if)
Conditional display (using v-show)
Dynamic components
Component root nodes
So I arrange my code to have a v-if in my v-for.
Here a piece of code :
<transition name="fade">
<img id='pvw' v-for='day in days' :src='day.preview' v-if='day.current' title='Preview' />
</transition>
And a little bit of css :
.fade-enter-active, .fade-leave-active {
transition: opacity .5s
}
.fade-enter, .fade-leave-active {
opacity: 0
}
Here is fiddle to see what I am trying to do.
Please help me to achieve this, not sure why it doesn't work.
Vue Transition Example Just choose the element that you want to transition and wrap it in a <transition> component. For this example, we're creating a button that toggles a <p> element to render. And the corresponding <script> section. Now, we just have to add some styles to actually get the transitions working.
v-for. We can use the v-for directive to render a list of items based on an array. The v-for directive requires a special syntax in the form of item in items , where items is the source data array and item is an alias for the array element being iterated on: js const items = ref([{ message: 'Foo' }, { message: 'Bar' } ...
<TransitionGroup> is a built-in component designed for animating the insertion, removal, and order change of elements or components that are rendered in a list.
<teleport> is a Vue 3 component that helps render HTML elements from one component to a different part of the code. It allows you to move an object from one place to another.
You have to use TRANSITION GROUP, not TRANSITION.
I would suggest to use something like: https://github.com/asika32764/vue2-animate which is great package which allows you to use CSS animations from AnimateCSS in your Vue application.
For example, in your case you would use something like that:
<transition-group name="fadeLeft" tag="div" >
<img v-for="day in days" :src='day.preview' v-bind:key="day" v-if='day.current' title='Preview'>
</transition-group>
You're doing v-if="day.current"
so I doubt that's ever going false
long enough for Vue to recognize it to do the transition. If you just add :key="day.preview"
to your image tag you'd be good to go.
https://jsfiddle.net/hfp78gfe/1/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With