这是我的代码:我想在每次数据更新时在 HelloWorld 组件上创建一个转换。过渡本身工作正常
<transition name="fade">
<p v-if="awesome">Vue is awesome</p>
</transition>
Run Code Online (Sandbox Code Playgroud)
这是我动态创建的“卡片”。
<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>
Run Code Online (Sandbox Code Playgroud)
此处过渡不起作用。
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>
Run Code Online (Sandbox Code Playgroud)
为什么以及如何让它工作?
如果要each
在循环中为元素设置动画,则必须:
transition
周围的循环。<transition-group>
,而不仅仅是<transition>
<v-row no-gutters>
<transition-group name="fade-in" 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>
Run Code Online (Sandbox Code Playgroud)
而且我还建议您不要使用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;
}
Run Code Online (Sandbox Code Playgroud)
如果动画焦躁,您可以添加position: absolute;
在enter
和leave-to
CSS规则(或仅leave-active
)。
在 vue 文档中查看此页面:https : //vuejs.org/v2/guide/transitions.html#List-Move-Transitions
归档时间: |
|
查看次数: |
3014 次 |
最近记录: |