动态组件切换的过渡

<template>
        <transition name="list" mode="out-in">
            <keep-alive>
                <component :is="currentView">
                </component>
            </keep-alive>
        </transition>
</template>
<style scoped>
.list-item {
  display: inline-block;
  margin-right: 10px;
}
.list-enter-active, .list-leave-active {
  transition: all 1s;
}
.list-enter, .list-leave-to
{
  opacity: 0;
  transform: translateY(30px);
}

</style>


当有相同标签名的元素切换时,需要通过 key 特性设置唯一的值来标记以让 Vue 区分它们

<transition :name="slide">
  <keep-alive>
    <component 
      :is="questionMap[currentQuestion.type]"
      :key="index"
      :currentQuestion="currentQuestion"
      :index="index">
    </component>
  </keep-alive>
</transition>



给组件添加了key=”index”了之后,不管任何类型都有过渡效果了,因为 
此时vue将每一个组件区分为不同的组件

问题2:先离开后另一个再进入。

同时生效的进入和离开的过渡不能满足所有要求,所以 Vue 提供了 过渡模式 
in-out:新元素先进行过渡,完成之后当前元素过渡离开。 
out-in:当前元素先进行过渡,完成之后新元素过渡进入。

因此我们需要在transition标签中添加mode来达成效果:

<transition :name="slide" mode="out-in">
  <keep-alive>
    <component 
      :is="questionMap[currentQuestion.type]"
      :key="index"
      :currentQuestion="currentQuestion"
      :index="index">
    </component>
  </keep-alive>
</transition>



如果要使用列表排序的话,需要使用transition-group

<template>
<div id="list-demo" class="demo">
  <button v-on:click="add">Add</button>
  <button v-on:click="remove">Remove</button>
  <transition-group name="list" tag="p">
    <span v-for="item in items" v-bind:key="item" class="list-item">
      {{ item }}
    </span>
  </transition-group>
</div>
</template>
<script>
new Vue({
  el: '#list-demo',
  data: {
    items: [1,2,3,4,5,6,7,8,9],
    nextNum: 10
  },
  methods: {
    randomIndex: function () {
      return Math.floor(Math.random() * this.items.length)
    },
    add: function () {
      this.items.splice(this.randomIndex(), 0, this.nextNum++)
    },
    remove: function () {
      this.items.splice(this.randomIndex(), 1)
    },
  }
})
</script>
<style>
.list-item {
  display: inline-block;
  margin-right: 10px;
}
.list-enter-active, .list-leave-active {
  transition: all 1s;
}
.list-enter, .list-leave-to
/* .list-leave-active for below version 2.1.8 */ {
  opacity: 0;
  transform: translateY(30px);
}
</style>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值