vue.jsvuejs3vue-transitions

vue fade transition only work when element is leaving and not entering


I am trying to learn Vue.js and doing a basic fade. I have an list of links that always stay on the page. On clicking the link, I wish to fade in and out the visibility of a div. By default the div isn't seen. This div contains several components. I tried to follow the documentation. However, the element only fades out while leaving, but nothing happens to it when enters. I've removed excess parts of this document and kept the relevant elements.

<template>
   <div class="list">
      <ul>
         <li @click="openWindPoetry">☞ Wind Poetry</li> // button that toggles div show
      </ul>
   </div>
   <div style="width: 97%;" class="project container">
      <transition name="fade" mode="out-in">
         <div v-if="showWP" class="backdrop" @scroll.passive="handleScroll"> // div needed to transition
            <Header theme="WP" header="Wind Poetry" :types="['Interaction','Data Visualisation','2020']"></Header>
            <BodyText theme="WP" heading="ABOUT"
               body="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam nulla magna, consectetur quis tellus eget, ultricies mattis nisi. Duis dictum dolor in ante placerat, non ultricies dui faucibus. Vivamus lobortis sapien porttitor, molestie urna ut, varius neque. In eu dapibus lectus. Etiam consequat, massa ut consequat lacinia, velit dui molestie dolor, id dapibus sem justo a ante. In pellentesque, odio ut pharetra congue, quam tellus efficitur arcu, non mattis risus nibh ac turpis.">
            </BodyText>
            <div class="introImg">
               <video alt="GIF DEMO" loop autoplay="autoplay" src="./assets/wind/wind_poetry-demo-1.mp4"></video>
            </div>
      </transition>
   </div>
</template>

<script>
export default {
  name: "App",
  components: {
    Header,
    Gradient,
    BodyText
  },
  data() {
    return {
      title: "LIST OF WORKS",
      ProjTitle: "Wind Poetry",
      showWP: false
    };
  },
  methods: {
    openWindPoetry() {
      (this.title = "BACK TO HOME"),
        (this.showWP = true),
        (this.showRec = false);
    }
  }
}
</script>

<style>
#app {
  font-family: "Roboto Mono";
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s ease-out;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}
</style>

Solution

  • In Vue 3, the transition class name has changed from -enter to -enter-from, so your classes should be renamed accordingly to allow the transition calculation to work properly:

    /* .fade-enter {/*...*/}  ❌ Vue 2 class name */
    .fade-enter-from {/*...*/}
    

    demo