Home » vue » vue – transition not working when hover for text changing

vue – transition not working when hover for text changing

Posted by: admin November 26, 2021 Leave a comment

Questions:

In the website, when you hover the circle area, the title and description in red box will change.

I want to have animation effect fade when the text changes, so I use <transition> to do it according to doc.

But I find it not working.

App.vue

<template>
  <div id="app">
    <div
      v-for="opt in options"
      :key="opt.id"
      class="item"
      @mouseover="changeCurrentOption(opt.id)"
    >
      {{ opt.name }}
    </div>
    <div class="text">
      <transition name="component-fade" mode="out-in">
        <div class="title">{{ options[currentOption - 1].name }}</div>
      </transition>
      <transition name="component-fade" mode="out-in">
        <div class="description">
          {{ options[currentOption - 1].description }}
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      options: [
        {
          id: 1,
          name: "Cat",
          description:
            "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
        },
        {
          id: 2,
          name: "Dog",
          description:
            "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).",
        },
      ],
      currentOption: 1,
    };
  },
  methods: {
    changeCurrentOption(id) {
      this.currentOption = id;
    },
  },
};
</script>

<style lang="scss">
#app {
  text-align: center;
  margin-top: 60px;
  .item {
    width: 150px;
    height: 150px;
    border-radius: 50%;
    border: 1px solid black;
    margin-bottom: 1rem;
  }
  .text {
    border: 1px solid red;
  }
}
</style>

enter image description here

CodeSandbox:

https://codesandbox.io/s/hardcore-montalcini-dcyi2?file=/src/App.vue

Answers:

I most use correct syntax for transition
check vue.js document about this
Enter/Leave & List Transitions

and I fixed your code here

<template>
  <div id="app">
    <div
      v-for="opt in options"
      :key="opt.id"
      class="item"
      @mouseover="changeCurrentOption(opt.id)"
      @mouseleave="mouseleaveHandel"
    >
      {{ opt.name }}
    </div>
    <div class="text">
      <transition name="fade" mode="out-in">
        <div v-if="descriptionVisibale">
          <div class="title">{{ options[currentOption - 1].name }}</div>

          <div class="description">
            {{ options[currentOption - 1].description }}
          </div>
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      descriptionVisibale: true,
      options: [
        {
          id: 1,
          name: "Cat",
          description:
            "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
        },
        {
          id: 2,
          name: "Dog",
          description:
            "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)."
        }
      ],
      currentOption: 1
    };
  },
  methods: {
    changeCurrentOption(id) {
      if (this.currentOption !== id) {
        this.currentOption = id;
        this.descriptionVisibale = false;
        setTimeout(() => {
          this.descriptionVisibale = true;
        }, 1000);
      }
    },
    mouseleaveHandel() {
      this.descriptionVisibale = true;
    }
  }
};
</script>

<style lang="scss">
#app {
  text-align: center;
  margin-top: 60px;
  .item {
    width: 150px;
    height: 150px;
    border-radius: 50%;
    border: 1px solid black;
    margin-bottom: 1rem;
  }
  .text {
    border: 1px solid red;
    min-height: 5rem;
    padding: 1rem;
  }
}
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}
</style>

I think finally complete your issues you most use setTime Out like this :
setTimeout()