在Vue和Vuetify中,我想使用animate.css
动态更改v卡,但我遇到了问题。 out-in
模式在这种情况下不起作用。淡入和淡出动画同时移动。结束淡入后如何强制其开始淡入动画?
new Vue({
el: '#app',
data() {
return {
number: 1,
items: [
{
text: "a",
number: 1
},
{
text: "b",
number: 2
},
{
text: "c",
number: 3
},
]
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script><script src="https://unpkg.com/vuetify/dist/vuetify.min.js"></script>
<link href="https://unpkg.com/vuetify/dist/vuetify.min.css" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/npm/animate.css" rel="stylesheet"/>
<div id="app">
<div v-for="(item, index) in items" :key="index">
<transition mode="out-in" enter-active-class="animated slideInLeft" leave-active-class="animated slideOutRight">
<v-card dark class="ma-3" v-if="number === item.number">
<p>{{item.text}}</p>
</v-card>
</transition>
</div>
<br>
<v-btn @click="number++; if(number === 4) number = 1;">Next</v-btn>
</div>
答案 0 :(得分:2)
您的过渡位于一个循环内,这意味着在渲染之后,您将有多个transitions
彼此不相关(mode
不起作用)...这就是您的模板的方式渲染后的样子:
<div key="0">
<transition mode="out-in" enter-active-class="animated slideInLeft" leave-active-class="animated slideOutRight">
<v-card dark class="ma-3" v-if="true">
<p>a</p>
</v-card>
</transition>
</div>
<div key="1">
<transition mode="out-in" enter-active-class="animated slideInLeft" leave-active-class="animated slideOutRight">
<!-- <v-card dark class="ma-3" v-if="false">
<p>b</p>
</v-card> -->
</transition>
</div>
<div key="2">
<transition mode="out-in" enter-active-class="animated slideInLeft" leave-active-class="animated slideOutRight">
<!-- <v-card dark class="ma-3" v-if="false">
<p>c</p>
</v-card> -->
</transition>
</div>
<div key="3">
<transition mode="out-in" enter-active-class="animated slideInLeft" leave-active-class="animated slideOutRight">
<!-- <v-card dark class="ma-3" v-if="false">
<p>d</p>
</v-card> -->
</transition>
</div>
因此,当您按next键时,您正在从过渡过渡到另一过渡...在这种情况下,mode
丢失了。
我们只需要将整个项目包装在一个过渡中,因此在这种情况下,只有一个transition
可以检测元素何时出库并且有新元素进入(mode
会起作用):
Vue.config.devtools = false;
Vue.config.productionTip = false;
new Vue({
el: '#app',
data() {
return {
number: 1,
items: [{
text: "a",
number: 1
},
{
text: "b",
number: 2
},
{
text: "c",
number: 3
},
]
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/vuetify/dist/vuetify.min.js"></script>
<link href="https://unpkg.com/vuetify/dist/vuetify.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/animate.css" rel="stylesheet" />
<div id="app">
<transition mode="out-in" enter-active-class="animated slideInLeft" leave-active-class="animated slideOutRight">
<template v-for="(item, index) in items">
<v-card dark class="ma-3" v-if="number === item.number" :key="index">
<p>{{item.text}}</p>
</v-card>
</template>
</transition>
<br>
<v-btn @click="number++; if(number === 4) number = 1;">Next</v-btn>
</div>
答案 1 :(得分:1)
在使用<transition-group>
循环进行迭代时,应使用v-for
。
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js">
</script><script src="https://unpkg.com/vuetify/dist/vuetify.min.js"></script>
<link href="https://unpkg.com/vuetify/dist/vuetify.min.css" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/npm/animate.css" rel="stylesheet"/>
<div id="app">
<transition-group mode="out-in" enter-active-class="animated slideInLeft" leave-active-class="animated slideOutRight">
<div v-for="(item, index) in items" :key="index">
<v-card dark class="ma-3" v-if="number === item.number">
<p>{{item.text}}</p>
</v-card>
</transition-group>
</div>
<br>
<v-btn @click="number++; if(number === 4) number = 1;">Next</v-btn>
</div>
文档:https://vuejs.org/v2/guide/transitions.html#List-Transitions