vue实现页面切入淡入淡出、文字淡入淡出效果<transition>标签

二、使用条件

满足以下条件之一

  • 条件渲染 (使用 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


版权声明:本文为weixin_46353030原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。