首页 > 解决方案 > vue - 悬停以更改文本时转换不起作用

问题描述

在网站中,当您将鼠标悬停在圆圈区域时,红色框中的和会发生变化titledescription

我想fade在文本变化时有动画效果,所以我习惯<transition>按照doc来做。

但我发现它不起作用。

应用程序.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>

在此处输入图像描述

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

标签: javascripthtmlcssvue.js

解决方案


我最常使用正确的语法来检查有关此Enter/Leave & List Transitions
的 vue.js 文档

我在这里修复了你的代码

<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>

我认为最终完成您最常使用 setTime Out 的问题,如下所示: setTimeout()


推荐阅读