Vue transition not functioning properly when hovering over text for changes

When you hover over the circular region on the website, the text and description in the red box will change.

To add a fade animation effect when the text changes, I attempted to use <transition> as per the documentation provided in this link.

Unfortunately, the transition effect is not working as expected.

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... // (Continues similarly...)
        },
        // Additional data for Dog may be found here...
      ],
      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

Answer №1

To ensure smooth transitions, it is important to use the correct syntax for transition. For more information on this, refer to the Vue.js documentation: Enter/Leave & List Transitions

I have made corrections to your code below:

<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...."
        },
        {
          id: 2,
          name: "Dog",
          description:
            "It is a long established fact that a reader will be distracted by the readable content..."
        }
      ],
      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>

To resolve any remaining issues, you may need to utilize `setTimeout()` as shown here: setTimeout()

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

React Express Error: Unable to access property 'then' of undefined

I'm facing an issue while trying to server-side render my react app for users who have disabled JavaScript and also for better search engine optimization. However, I am encountering the following error: TypeError: Cannot read property 'then' ...

Title of Flickr API

Hey everyone, I'm working on setting up a gallery on my website using images and sets from Flickr. I've successfully loaded all the sets with the following code: $flickr = simplexml_load_file('http://api.flickr.com/services/rest/?method=fli ...

React: Improve performance by optimizing the use of useContext to prevent unnecessary re-renders of the entire app or efficiently share data between components without causing all

In my app, I have a Header.tsx component that needs to be accessible on all pages and a Home.tsx component where most of the content resides. The Home.tsx component includes an intersectionObserver that utilizes the useContext hook (called homeLinks) to p ...

Is it a good idea to relocate the document.ready function into its own JavaScript function?

Can the following code be placed inside a separate JavaScript function within a jQuery document.ready, allowing it to be called like any other JavaScript function? <script type="text/javascript"> $(document).ready(function() { $('div#infoi ...

Remove buttons from carousel elements in React Multi-Carousel

Is there a way to hide the arrows in the react-multi-carousel component? https://i.stack.imgur.com/V1nix.png ...

Creating an HTML tag from Angular using TypeScript

Looking at the Angular TypeScript code below, I am trying to reference the divisions mentioned in the HTML code posted below using document.getElementById. However, the log statement results in null. Could you please advise on the correct way to reference ...

Error: Uncaught TypeError - The function indexOf is not defined for e.target.className at the mouseup event in HTMLDocument (translator.js:433) within the angular

Upon clicking on an SVG to edit my data in a modal bootstrap, I encountered the following error: Uncaught TypeError: e.target.className.indexOf is not a function at HTMLDocument.mouseup (translator.js:433) This is my SVG code: <svg data-dismiss ...

Troubleshooting a text alignment problem with form-group in the Bootstrap framework

Currently, I am in the process of building a form that consists of several form-group elements. My main issue arises when dealing with long text as it causes alignment problems. Could you kindly take a look at the problem through this link: http://jsfidd ...

What methods can be used to ensure a required minimum delay time between function executions?

For my node function, I am aiming for a specific execution delay of around 5 seconds. The minimum delay needs to be implemented within the function itself, not externally, so external users are unaware of the delay. Therefore, I cannot rely on modules l ...

Problem with javascript querystring code

Currently in the process of writing some vanilla JavaScript code to manipulate query strings without using jQuery. Here's what I have so far: // If there is no viewid in the query string, append it as a new key if (newUrl.indexOf("viewid") == -1) { ...

The issue of jQuery not loading within Ajax content has been resolved

Appreciate the assistance provided. I am facing an issue where my jQuery code does not load within ajax content. Below is the code snippet from index.html : <script language="javascript" type="text/javascript"> $(function() { $("#gps").load("find. ...

A program designed to access and retrieve a universal variable

It seems like a simple task, but I can't seem to figure it out. I'm trying to assign the currentUserId within the getCurrentUser function so that I can use it in other functions. Currently, the code below is returning undefined. What am I overl ...

I am unable to utilize the map function as it results in a TypeError: The property 'map' is undefined and cannot be read

I recently delved into the world of "REACT" in order to work on the front-end of my own project. I encountered a problem for 2 days with the map function while getting data from my back-end and saving the IDs in the Cuartos array. I couldn't figure ou ...

Retrieve the public URL from the index.html file following the build process

In my project, I utilized ReactJS by creating an app with the command npx create-react-app my-app. To build the project, I used yarn build. Within the package.json file, the scripts section appears as follows: "scripts": { "start": "react-scripts sta ...

What are some strategies for preventing unused setState functions in React? Is it possible to create a React useState without a setter function

Currently working on reducing or eliminating npm warnings related to the React site. Many of these warnings are due to the setState function, which is marked as 'unused' in the code snippet below. const [state, setState] = useState('some st ...

JavaScript event manually triggered not propagating within an element contained in an iFrame context

I am currently developing a WYSIWYG designer that enables users to choose colors through [input type="color"] fields. The interface includes inputs on the left side and an iFrame on the right side displaying the generated preview. Normally, when ...

Implementing a variety of threshold colors in Highcharts

Exploring this Highcharts fiddler: http://jsfiddle.net/YWVHx/97/ I'm attempting to create a similar chart but encountering some issues. The fiddler I'm currently working on is here: (check out the edit below!) The key difference in functionalit ...

Encountering difficulties when attempting to load a module with the "js" extension in a TypeScript environment

When making a GET request with Systemjs, the extension .js is not being added to the URL. These are my TypeScript Classes customer.ts import {Address} from "./Address"; export class Customer { private _customerName: string = ""; public Customer ...

Select dropdown options in Material UI are overlapping

Exploring React for the first time and utilizing material-ui's select button feature. It displays highlighted text based on user input, but ideally this highlight should disappear upon selection of an element. https://i.stack.imgur.com/2ccaS.png How ...

Why is my PHP function not able to properly receive the array that was sent to it via Ajax?

After retrieving an array through an ajax query, I am looking to pass it to a PHP function for manipulation and utilization of the elements at each index. The PHP function in question is as follows: class ControladorCompraEfectivoYTarjeta { public fu ...