Vue - Vue中多个元素或组件的过渡

一. Vue中多个元素的过渡

transition 标签 mode 属性:

  • in-out:多个元素切换的时候,先显示再隐藏
  • out-in:多个元素切换的时候,先隐藏再显示
<template>
  <div class="wrap">
    <transition mode="out-in">
      <div v-if="show" key="hello">hello</div>
      <div v-else key="world">world</div>
    </transition>
    <button @click="handleClick">切换显隐</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      show: true,
    };
  },
  methods: {
    handleClick() {
      this.show = !this.show;
    },
  },
};
</script>
<style scoped>
.v-enter,.v-leave-to{
  opacity: 0;
}
.v-enter-active,.v-leave-active{
  transition: opacity 1s;
}
</style>

二. Vue中多个组件的过渡

父组件

<template>
  <div class="wrap">
    <h2>Vue中多个元素或组件的过渡</h2>
    <transition mode="out-in">
      <component :is="type"></component>
    </transition>
    <button @click="handleClick">切换显隐</button>
  </div>
</template>

<script>
//子组件地址仅供参考
import One from "../child/ChildOne.vue";
import Two from "../child/ChildTwo.vue";
export default {
  components: {
    One,
    Two,
  },
  data() {
    return {
      type: 'One',
    };
  },
  methods: {
    handleClick() {
      this.type = this.type == "One" ? "Two" : "One";
    },
  },
};
</script>
<style scoped>
.v-enter,
.v-leave-to {
  opacity: 0;
}
.v-enter-active,
.v-leave-active {
  transition: opacity 1s;
}
</style>

第一个子组件

<template>
  <div class="wrap">
      组件1
  </div>
</template>
<script>
export default {
  data() {
    return {};
  },
};
</script>
<style scoped>
</style>

第二个子组件

<template>
  <div class="wrap">
      组件2
  </div>
</template>
<script>
export default {
  data() {
    return {};
  },
};
</script>
<style scoped>
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值