Activate a specific component of hover effect within multiple components generated by the `v-for` loop

Hey there! I'm currently facing a CSS challenge related to Vue. Let's say I want to display multiple components using v-for, and I want to add a hover effect to each component individually. The goal is to have the hover effect only apply to the component being hovered over, without affecting the other components. However, in my current implementation below, when I hover over one component, all components are affected. So, my question is how can I achieve the desired result?

<component v-for="(item, index) in items" :key="index">
  <div @mouseover="hover = true" @mouseleave="hover = false">
     <div v-if="hover" class="xx"> Hover Effect </div>
     <div v-else> Normal Effect </div>
  </div>
</component> 

Answer №1

To solve this issue, it is crucial to keep a record of the index or key of the element being hovered over and then clear it once you move away. You have the option to create two separate methods specifically for handling this task.

<component v-for="(item, index) in items" :key="index">
 <div @mouseover="hoveredIndex = index" @mouseleave="hoveredIndex = -1">
  <div v-if="hoveredIndex===index" class="xx"> Hover Effect </div>
    <div v-else> Normal Effect </div>
   </div>
</component> 

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

As you scroll, the top block in each of the three columns will remain fixed within its

I need assistance with a problem involving three columns and multiple blocks within each column. Specifically, I want the first block in each column to remain fixed at the top when scrolling. However, once you reach the bottom of each column, the first blo ...

Utilizing external Cascading Style Sheets in AngularJS 1

Can an external CSS be applied to a component in AngularJS1? If yes, then how can it be done? I have only seen examples of applying inline css... ...

CSS Grid with dynamically changing number of rows set to "auto", while ensuring one row always has a fixed size of "1fr"

Currently, I am experimenting with a CSS grid-based frontend and have encountered a specific requirement that keeps repeating itself in various parts of the frontend: A grid with a dynamic number of rows. Each row should be of variable size (auto). The f ...

Book of Tales displaying Emotion Styling upon initial load only

I'm currently working with the styled api provided by emotion in order to create a custom styled button. const StyledButton = styled(Button)` background-color: ${theme.palette.grey['800']}; width: 50; height: 50; &:hover { ba ...

Encountered a problem while trying to start a new project using vue

I encountered an error when running "vue create project_name". Click here to see the error message. Is there anyone who can assist me in resolving this issue? I have attempted re-installing both Node.js and Vue, but the problem persists. Just for your in ...

Fontawesome is unable to update the class due to the presence of invalid characters in the string

const toggleDarkOrLight = document.getElementsByTagName('i')[0]; var toggled = false; const toggleFunction = () => { if (toggled === false) { toggleDarkOrLight.classList.remove('fa fa-toggle-off'); toggleDarkOrLight.classLi ...

Tips on managing canvas overflow within a vuetify card and ensuring responsiveness

Seeks Responsive Design Solution for fabricjs Canvas Inside Vuetify Card Hello, I am currently struggling to implement a responsive fabricjs canvas inside vuetify and ensure that it adjusts properly on all screen sizes. Right now, the canvas is overflowin ...

Trouble encountered while attempting to choose a single checkbox from within a v-for loop in Vue.js?

<div id="example-1"> <ul> <input type="text" v-model="searchString" placeholder="Filter" /> <p>sortKey = {{sortKey}}</p> <li v-for="item in sortedItems"> <input class="checkbox-align" type="checkbo ...

Embedding individual files as components

I am contemplating the possibility of migrating an AngularJS (1.x) webapp to vue.js. This application features numerous "widgets" on its main page <script optimize-inline src="data/widgets/solargraph/solargraph.js"></script> <script optimi ...

Manipulating CSS rules using JavaScript/jQuery

My goal is to create a function that generates a grid based on table data. While the function itself seems to be working, I am encountering an issue where the classes applied are not resulting in any style changes. Below is a snippet of my function: $(doc ...

How to position footer at the bottom of Material UI cards - see example below

After getting inspiration from the material-ui example of cards, I wanted to create a grid layout with multiple cards. My goal was to make all the cards have equal height (which I achieved using height:100%) and position the footer at the bottom of each ca ...

The act of selecting a parent element appears to trigger the selection of its child elements as well

I am attempting to create an accordion using Vanilla JavaScript, but I have encountered an issue. When there is a child div element inside the header of the accordion, it does not seem to work and I'm unsure why. However, if there is no child div elem ...

Ways to perfectly align three images side by side using CSS

I am trying to align 3 pictures horizontally, but they keep ending up on separate lines. Here is the code I have been using: HTML <section class="features"> <figure> <img src="....jpg" alt="..."> ...

What is the formula to determine the precise hue-rotate needed for producing a particular shade?

I'm looking to change the color of a white background image in a div to match the main theme color. I know I can use filters like sepia(), saturate(10000%), and hue-rotate() to achieve this, but is there a way to calculate these values beforehand? Sin ...

A method to trigger the opening of a div tag when a button is clicked using Vue.js

<div class="input-wrapper"> <div class="mobile-icon"></div> <input class="input-section label-set" type="text" v-model.trim="$v.mobile.$model" :class="{'is-invalid': ...

Using Vuetify to submit form data using Axios

Utilizing Vuetify in my VueJs application, I am trying to send data from a form that includes file (CSV) uploads and various number inputs. My goal is to achieve this using Axios. Despite several attempts, I keep encountering a 404 error. This snippet sh ...

Running a function in a different vue file from an imported file - A step-by-step guide

Learning the ropes of vue.js, I am working with two separate vue files: First file: import second from second.vue; export default{ component: {second}, data() {return{ data: 'data', }} methods: { function f1{ console.log(t ...

`Cannot Get jQuery ScrollTop Function to Work for 2nd Element`

Having trouble with an issue. Let me explain. I implemented a scrollTop Jquery on page load that scrolls down after a delay to prompt user action. However, I'm facing a problem where another scrollTop doesn't work after clicking buttons "#b3,#b3 ...

Creating Stunning CSS Animations for a Slider Using SVG

Looking for help to create an SVG CSS animation for a slider using a mockup image. The animation will feature a balloon with a number value that zooms in and out from left to right or right to left. Currently stuck at the starting point and unsure how to ...

Set up global variables for components to access

Currently, I am working on a Laravel 8 project with vue-sweetalert2 integration. My goal is to set up the Toast behavior once and then be able to call it within various components. At the moment, my code looks like this: /js/components/Mypage.vue <scr ...