vue中多个元素或多个组件之间的动画效果
2024-08-30 23:20:31
多个元素的过渡
<style>
.v-enter,.v-leave-to{
opacity: 0;
}
.v-enter-acitve,.v-leave-active{
opacity: opacity 1s;
}
</style>
<div id='app'>
<transition>
<div v-if='show'>hello world</div>
<div v-else>bye world</div>
</transition>
<button @click='handleClick'>切换</button>
</div>
<script>
var vm = new Vue({
el:'#app',
data:{
show:true
},
methods:{
handleClick:function(){
this.show = !this.show;
}
}
})
</script>
按照之前的写法方式,渐隐渐出的效果并没有出现该有的效果,那么为什么呢?
在if else两个元素切换的时候,会尽量的复用dom,正是vue,dom的复用,导致动画的效果不会出现,如果想要vue不去复用dom,之前也说过,怎么做呢,给两个div不同的key值就行了
<div v-if='show' key='hello'>hello world</div>
<div v-else key='bye'>bye world</div>
这样就可以有个明显的动画效果,,多个元素过渡动画的效果
transition还提供了一个mode属性,in-out是先显示再隐藏,out-in是先隐藏再显示
多个组件的过渡
<style>
.v-enter, .v-leave-to {
opacity: 0;
}
.v-enter-acitve, .v-leave-active {
transition: opacity 1s;
}
</style>
<div id='app'>
<transition mode='out-in'>
<child v-if='show'></child>
<child-one v-else></child-one>
</transition>
<button @click='handleClick'>切换</button>
</div> <script>
Vue.component('child',{
template:'<div>child</div>'
})
Vue.component('child-one',{
template:'<div>child-one</div>'
})
var vm = new Vue({
el:'#app',
data:{
show:true
},
methods:{
handleClick:function(){
this.show = !this.show;
}
}
})
</script>
这个就是多个组件的过渡,采用的是上面的方式,替换子组件,那么我们换一种方式,用动态组件
<style>
.v-enter, .v-leave-to {
opacity: 0;
}
.v-enter-acitve, .v-leave-active {
transition: opacity 1s;
}
</style>
<div id='app'>
<transition mode='out-in'>
<component :is='type'></component>
</transition>
<button @click='handleClick'>切换</button>
</div> <script>
Vue.component('child',{
template:'<div>child</div>'
})
Vue.component('child-one',{
template:'<div>child-one</div>'
})
var vm = new Vue({
el:'#app',
data:{
type:'child'
},
methods:{
handleClick:function(){
this.type = (this.type === 'child' ? 'child-one' : 'child')
}
}
})
</script>
这样也实现了多个组件的过渡效果
最新文章
- Filco minila 的蛋疼。
- Linux - 获取Shell命令帮助信息
- [开发笔记]-Windows Service服务相关注意事项
- [React] Higher Order Components (replaces Mixins)
- HDU 4135 Co-prime
- java获取当前方法
- MySQL的三层架构之一----连接层
- 1005. 继续(3n+1)猜想 (25) (ZJUPAT 数学)
- (转)java并发之Executor
- SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建
- Vue 爬坑之路(八)—— 使用 Echarts 创建图表
- ie兼容问题记录
- tensorflow学习笔记1:导出和加载模型
- List does not exist. The page you selected contains a list that does not exist. It may have been deleted by another user
- python 中 打印及格式化字符串的相关方法
- python+ajaxFileUpload 无刷新上传文件
- Linux 命令之删除命令
- [UOJ#276][清华集训2016]汽水[分数规划+点分治]
- 71. Simplify Path(M)
- 关于面试总结2-SQL学生表