这是我的代码:每当数据更新时,我都想在HelloWorld组件上创建一个过渡。过渡本身工作正常
<transition name="fade">
<p v-if="awesome">Vue is awesome</p>
</transition>
这是我动态创建的“卡片”。
<v-row no-gutters>
<v-col cols="12" sm="4" md="4" v-for="(todo, index) in todos" v-bind:key="index">
<v-card class="pa-2" outlined tile>
<transition name="fade">
<HelloWorld
v-bind:todos="todos"
v-bind:index="index"
v-bind:class="(todos[index].done)?'green':'red'"
/>
</transition>
</v-card>
</v-col>
</v-row>
此处过渡不起作用。
CSS在这里:
<style scoped>
.top {
background: blue;
color: white;
display: flex;
justify-content: space-around;
border-bottom: 2.5px solid black;
}
.fade-enter {
opacity: 0;
}
.fade-enter-active {
transition: opacity 1s;
}
.fade-leave {
}
.fade-leave-active {
transition: 1s;
opacity: 0;
}
</style>
为什么以及如何完成工作?
答案 0 :(得分:2)
如果要在循环中为each
元素设置动画,则必须:
transition
放在循环中。 <transition-group>
,而不只是<transition>
<v-row no-gutters>
<transition-group name="fade" mode="out-in">
<v-col cols="12" sm="4" md="4" v-for="(todo, index) in todos" v-bind:key="index">
<v-card class="pa-2" outlined tile>
<HelloWorld
v-bind:todos="todos"
v-bind:index="index"
v-bind:class="(todos[index].done)?'green':'red'"
/>
</v-card>
</v-col>
</transition-group>
</v-row>
我也建议您不要使用1s
长动画,因为它太长了。做这样的事情:
.fade-in-enter-active {
transition: all 0.5s ease;
}
.fade-in-leave-active {
transition: all 0.5s ease;
}
.fade-in-enter, .fade-in-leave-to {
position: absolute; /* add for smooth transition between elements */
opacity: 0;
}
如果动画有点跳动,则可以在position: absolute;
和enter
CSS规则中添加leave-to
(或仅对leave-active
添加)。
在vue文档中签出此页面:https://vuejs.org/v2/guide/transitions.html#List-Move-Transitions
答案 1 :(得分:1)
<v-row no-gutters>
<transition-group name="fade" class="row">
<v-col cols="12" sm="4" md="4" v-for="(todo, index) in todos" :key="todo.randomKey">
<v-card class="pa-2" outlined tile>
<HelloWorld
v-bind:todos="todos"
v-bind:index="index"
v-bind:class="(todos[index].done)?'green':'red'"
/>
</v-card>
</v-col>
</transition-group>
</v-row>
您需要进行多次修改:
<transition-group>
代替<transition>
。<v-col>
包装<transitiono-group>
。class="row"
添加到<transition-group>
。index
例如todo.id
或todo.randomKey
。使用index
作为键就像
根本不用钥匙。