二、使用条件

满足以下条件之一

  • 条件渲染 (使用 v-if)
  • 条件展示 (使用 v-show)
  • 动态组件
  • 组件根节点

三、使用步骤

  1. 文字淡入淡出效果使用步骤
<template>
<div id="demo">
  <button v-on:click="show = !show">
    Toggle
  </button>
  <transition name="fade">
    <p v-if="show">hello</p>
  </transition>
</div>
</template>
<script>
new Vue({
  el: '#demo',
  data: {
    show: true
  }
})
</script>
<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity .5s;
}
.fade-enter, 
.fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}
</style>
  1. 页面切换时页面内容淡入淡出,需要先配置好路由以实现页面切换
  • App.vue
<template>
  <div id="app">
    <transition name="fade">
      <router-view></router-view>
    </transition>
  </div>
</template>

<script>
export default {
  name: "App",
};
</script>

<style lang="less">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
.fade-enter {
  opacity: 0;
}

.fade-leave {
  opacity: 1;
}

.fade-enter-active {
  transition: opacity 0.2s;
}

.fade-leave-active {
  opacity: 0;

  transition: opacity 0.2s;
}
</style>
  • Index.vue
<template>
  <div class="Index">
    <div class="nav">
      <button @click="docs">Go</button>
    </div>
  </div>
</template>
<script>
export default {
  name: "Index",
  data() {
    return {
      hide: true,
      show: false,
    };
  },
  methods: {
    docs() {
      console.log("check");
      this.$router.push("/HelloWorld");
    },
}
</script>
  • HelloWorld.vue
<template>
  <div><h1 @click="back">Back</h1></div>
</template>
<script>
export default {
  name: "HelloWorld",
  methods: {
    back: function () {
      this.$router.push("/Index");
    },
  },
};
</script>

参考:https://cn.vuejs.org/v2/guide/transitions.html

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐