vue组件切换时使用过渡(transition)时应该注意的一些事项

最近在做公司项目时,涉及到做一个问卷调查,需要在不同题目题目之间使用transition产生一个过渡效果:当点击按钮进入下一题时,当前卡片从正中间向左滑动至不可见后,下一题对应的卡片从右边不可见区域从右向左滑动至屏幕正中间,在这个过程中我发现了几个问题,下面是该部分对应的代码

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

问题1:不同类型的组件之间切换,有过渡效果,而相同组件(不同内容)切换则没有过渡效果

vue官网的描述:当有相同标签名的元素切换时,需要通过 key 特性设置唯一的值来标记以让 Vue 区分它们,否则 Vue 为了效率只会替换相同标签内部的内容。即使在技术上没有必要,给在 组件中的多个元素设置 key 是一个更好的实践。
改进后代码

<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,下面是一个简单的例子

<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>
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)
    },
  }
})
.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);
}
  • 1
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值