斯巴达克斯第三季02,浮沉2崔曼莉,定本育儿百科 下载
本文实例为大家分享了vue实现多个元素或多个组件之间动画的具体代码,供大家参考,具体内容如下
多个元素的过渡
<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>
这样也实现了多个组件的过渡效果。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
VUE+elementui组件在table-cell单元格中绘制微型echarts图
Vue通过getAction的finally来最大程度避免影响主数据呈现问题
vue 路由懒加载中给 Webpack Chunks 命名的方法
网友评论