Vue中多个元素、组件的过渡及列表过渡的方法示例
多个元素之间过渡动画效果
多元素之间如何实现过渡动画效果呢?看下面代码
.fade-enter,
.fade-leave-to{
opacity:0;
}
.fade-enter-active,
.fade-leave-active{
transition:opacity3s;
}
helloworld 多个元素之间过渡动画效果
多元素之间如何实现过渡动画效果呢?看下面代码
.fade-enter,
.fade-leave-to{
opacity:0;
}
.fade-enter-active,
.fade-leave-active{
transition:opacity3s;
}
helloworld 这么写行不行呢?肯定是不行的,因为Vue在两个元素进行切换的时候,会尽量复用dom,就是因为这个原因,导致现在动画效果不会出现。
如果不让Vue复用dom的话,应该怎么做呢?只需要给这两个div不同的key值就行了
helloworld
这个时候当div元素进行切换的时候,就不会复用了。
mode
Vue提供了一mode属性,来实现多个元素切换时的效果
mode取值in-out,动画效果是先出现在隐藏
//第一次点击时,执行顺序为:①② helloworld//再消失② byeworld//先显示①
mode取值为out-in,动画效果为先隐藏在出现
//第一次点击时,执行顺序为:①② helloworld//先消失① byeworld//再显示②
多个组件之间过渡动画效果
这里需要借助动态组件来实现多组件之间过渡动画效果
先用普通的方式来实现切换:
.fade-enter,
.fade-leave-to{
opacity:0;
}
.fade-enter-active,
.fade-leave-active{
transition:opacity1s;
}
Vue.component('child-one',{
template:'child-one'
})
Vue.component('child-two',{
template:'child-two'
})
letvm=newVue({
el:'#root',
data:{
show:true
},
methods:{
handleClick(){
this.show=!this.show
}
}
})
你会发现,这样子实现组件切换,transition动画效果是存在的,但是我们想要用动态组件来实现,该怎么弄呢?
可查看之前的文章:Vue动态组件与v-once指令,这篇文章中详细的介绍了Vue的动态组件
列表过渡
这里需要使用一个新标签transition-group来是实现
.fade-enter,
.fade-leave-to{
opacity:0;
}
.fade-enter-active,
.fade-leave-active{
transition:opacity1s;
}
{{item.title}}-----{{item.id}}
letvm=newVue({
el:'#root',
data:{
count:0,
list:[]
},
methods:{
handleClick(){
this.list.push({
id:this.count++,
title:'helloworld'
})
}
}
})
为什么使用了transition-group标签后,就可以出现过渡动画效果了呢?看下面代码:
helloworldhelloworldhelloworld
在循环过后,页面中会出现一个个div元素,如果你在外面添加一个transition-group的标签,相当于你在每一个div外层都加了一个transition标签,看下面代码
helloworld helloworld helloworld
这时候,Vue把列表的过渡转化为单个的div元素的过渡了,Vue会在这个元素隐藏或者显示的时候动态的找到时间点,增加对应的class。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。