Challenges with hover effects in Vue JS

When hovering over one of two images, a specific component is displayed. Hovering over the first image reveals a component with a red background, while hovering over the second image reveals a component with a yellow background.

My actual code differs greatly from this example. In my real project, instead of a simple yellow component, a complex stylized component with data is displayed. The issue I'm facing is that when I try to interact with the red or yellow components, they disappear. I want to be able to click on a button within these components, but due to their vanishing act, it's impossible. I experimented with applying @mouseout to the dropdown components, but it messed up the code structure as the mouse hover functionality was already faulty.

I know my explanation might be unclear, but I hope you can grasp my problem. Here's the link to my project on CodeSandbox for reference.

myothercomponent.vue

<template>
  <div>
    <p>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Explicabo
      accusamus quod quis voluptatibus dolor magni, pariatur accusantium fugit
      itaque sint
    </p>
    <button>Button</button>
  </div>
</template>

<script>
export default {
  name: "myOtherComponent",
};
</script>

myycomponent.vue

<template>
  <div>
    <p>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Explicabo
      accusamus quod quis voluptatibus dolor magni, pariatur accusantium fugit
      itaque sint
    </p>
    <button>Button</button>
  </div>
</template>

<script>
export default {
  name: "MyFirstComponent",
};
</script>

HelloWorld.vue

<template>
  <div>
    <div style="display: flex; justify-content: center">
      <div v-bind:key="index" v-for="(girl, index) in girls">
        <img
          style="width: 200px; height: 200px; margin: 5px"
          @mouseover="mouseOver(girl)"
          @mouseout="mouseout(girl)"
          v-bind:src="girl.imgSrc"
          alt="Snow"
        />
      </div>
    </div>

    <div v-for="(girl, index) in girls" v-bind:key="index">
      <slide-y-up-transition>
        <component
          v-show="girl.hovered"
          v-bind:is="girl.componentName"
        ></component>
      </slide-y-up-transition>
    </div>
  </div>
</template>

<script>
import { SlideYUpTransition } from "vue2-transitions";
import MyFirstComponent from "./colors/myycomponent";
import myOtherComponent from "./colors/myothercomponent";
export default {
  name: "HelloWorld",
  components: {
    MyFirstComponent,
    myOtherComponent,
    SlideYUpTransition,
  },
  data() {
    return {
      componentNames: ["MyFirstComponent", "myOtherComponent"],
      girls: [
        {
          imgSrc: "https://html5css.ru/css/img_lights.jpg",
          hovered: false,
          hoverColor: "#337700",
          componentName: "MyFirstComponent",
        },
        {
          imgSrc: "https://html5css.ru/css/img_lights.jpg",
          hovered: false,
          hoverColor: "#123456",
          componentName: "myOtherComponent",
        },
      ],
    };
  },

  methods: {
    mouseOver: function (girl) {
      girl.hovered = true;
    },

    mouseout: function (girl) {
      girl.hovered = false;
    },
  },
};
</script>

Answer №1

It seems like the solution to your problem may be changing the @mouseout event to @mouseleave event and testing if it behaves in the way you want.

<template>
<div>
 <div style="display: flex; justify-content: center">
  <div v-bind:key="index" v-for="(girl, index) in girls">
    <img
      style="width: 200px; height: 200px; margin: 5px"
      @mouseover="mouseOver(girl)"
      v-bind:src="girl.imgSrc"
      alt="Snow"
    />
  </div>
</div>

<div v-for="(girl, index) in girls" v-bind:key="index" @mouseleave="mouseout(girl)">
  <slide-y-up-transition>
    <component
      v-show="girl.hovered"
      v-bind:is="girl.componentName"
    ></component>
  </slide-y-up-transition>
</div>

If you also want to hide other colored blocks when hovering over a new image, you can try implementing the following:

mouseOver: function (girl) {
  this.girls.forEach((girl) => (girl.hovered = false));
  girl.hovered = true;
},

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

What is the best way to enable file downloads using Vue and Laravel?

I am working on implementing a feature to force download various types of files through Vue.js using Laravel as the backend. Despite my research, I have only come across solutions for forcing PDF downloads. What if there are multiple file formats that nee ...

What is the method for altering a CSS element's keyframe animation while it is currently running?

My little mouse speed detector, although not perfect, provides me with the current mouse speed every 100ms in the variable window.mouseSpeed.t. I decided to implement this feature because I wanted to create a whimsical animation at the bottom edge of the s ...

grid causing images to display incorrectly in incorrect positions

My project consists of 3 component files and a CSS file, but I am facing an issue where the Tiles are slightly off in their positioning towards the top left corner. Although no errors are being thrown, upon using the view grid feature in Firefox, it is ev ...

Having trouble with the installation of go get wails?

Attempting to implement golang and wails, but encountering issues after executing the following command: go get github.com/wailsapp/wails/cmd/wails Resulting in errors such as: ../../github.com/wailsapp/wails/cmd/semver.go:21:3: cannot use semverVers ...

Using slashes in the import statement versus using require statement

Note: I am currently running on node version 14.1 Here is the line of code I'm experimenting with: import "module-alias/register"; This results in the following error: Error [ERR_MODULE_NOT_FOUND]: Cannot find module The error seems to point to t ...

Clicking on ng-click doesn't result in opening new windows

I am experiencing an issue with a table where the buttons in the last column are supposed to open a new window but they are not working as expected. Below is the code snippet I am using: <table class="table table-hover" id="angular_table"> <th ...

Using Jquery to Switch Pages

Recently, I've been experimenting with using hashes to create animated transitions between pages. Interestingly, the first page that loads (the home page) fades in and out seamlessly. However, when I attempt to navigate to another page, specifically t ...

Here are the steps to trigger a pop-up window in JavaScript that is filled with data fetched via an AJAX call:

I'm currently working on a java class where I need to insert a link into a table grid. This code is specific to the internal API of our company. /***MyCustomLink.java*********/ customLink.setOnClick( "fetchURL", return false;); addGridItem("cu ...

PHP script to automatically update a table in a database upon the closure of a

I recently experimented with the following code snippet: <script> function myFunction() { return "You have not logged out of your account. Do you want to leave without logging out? "; } </script > <body onbeforeunload="return myFun ...

managing browser pop-ups in selenium with the help of JavaScript

My objective is to input a username and password in the popup box that shows up every time the page loads. I am utilizing selenium for this purpose, and unfortunately, all attempts I've made so far have been unsuccessful. I attempted to use the follo ...

Discover additional and subtract options

I am trying to implement a "See more" and "See less" button functionality for my list items inside an unordered list using the code below, but it is not working as intended. $(document).ready(function() { var list = $(".partners__ul li"); var numTo ...

Struggling to animate the selector of a nested div in Jquery?

Despite trying numerous variations, I am struggling to identify the correct selector of a nested div. The goal is to animate this particular div, but since no animations are taking place, I suspect that I have not selected the right element. Here is the a ...

Child component destruction triggers an ongoing digestion process

I am facing a strange issue within my AngularJS application. The error I'm encountering is the common $digest already in process error. Despite searching online and reviewing similar questions, none of the proposed solutions have resolved the issue. T ...

When using express, encountering a "Cannot GET / on page refresh" error

Currently working on a small MERN stack project. Managed to deploy it on Vercel successfully and the application runs as expected. Navigating to "/classes" and "/students" using the buttons in the browser works fine, however, upon reloading those pages I e ...

Rewriting Next.js with custom headers

My web app allows users to create their own profiles with custom usernames, which can be accessed via the following URLs. ourplatform.com/john ourplatform.com/john/about ourplatform.com/john/contact ourplatform.com/jane ourplatform.com/jane/about ourplatf ...

Javascript string manipulation operation

Is it possible to extract only the characters 'ABCD' from a string like 'ABCD150117T15' using JavaScript? I am interested in removing everything after 'ABCD' in this example, specifically excluding the first occurrence of a n ...

Learn how to incorporate slide transitions into the dropdown menu of a navbar in Bootstrap 4, complete with Vue.js transition effects

I'm attempting to incorporate a slide up and down transition into the bootstrap 4 dropdown menus using vue transitions. Unfortunately, the transition isn't functioning as expected. Even after utilizing the transition element, the animation stil ...

Sending an array variable from Vue to scssEnsure you transmit array variable

In my Vuejs3 project with Sass, I am retrieving a list of image names from an API and want to pass it to the SCSS to dynamically write styles with background-images. export default { name: 'TestComponent', data() { return { skills: ...

Ways to identify an element within a webpage

I'm currently working on creating a chrome extension that can detect the presence of specific elements on a webpage. The goal is to have this extension automatically run every time a new page is opened and display a popup message if the element is fou ...

Tips on enabling method calls and v-shows to function outside of Vue

We have integrated Vue.js for the popup feature in our application. The "Click" button within the Vue App triggers the popup as expected, but when attempting to call vueApp.methods.openModal() from outside, the popup does not appear even though the functio ...