vue实现多个元素或多个组件之间动画效果

发布时间:2020-09-25 19:52:07 作者:鸑鹄
来源:脚本之家 阅读:160

本文实例为大家分享了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>

这样也实现了多个组件的过渡效果。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. 多个线程之间的通信
  2. Vue中多个元素或组件的过渡

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue 元素 过渡

上一篇:浅谈vue同一页面中拥有两个表单时,的验证问题

下一篇:IOS中各种手势操作实例代码

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》