"Implement a feature in Vue.js where hovering over an element will dynamically add a

What is the best way to apply a class on a list item when hovering over it and remove the class when leaving?

<li class="test" @mouseenter="thumbStyle" @mouseleave="thumbStyle2">1</li>
<li class="test" @mouseenter="thumbStyle" @mouseleave="thumbStyle2">2</li>
<li class="test" @mouseenter="thumbStyle" @mouseleave="thumbStyle2">3</li>

In data & methods

data(){
  isActive:false
}

thumbStyle:function(){
   this.isActive = true;
},


thumbStyle2:function(){
   this.isActive = false;
},

I have attempted to do this, but only the first list element (li) gets the class applied when hovering over any list item. What technique should be used to target each individual li being hovered over? Similar to using $(this) in jQuery.

Answer №1

An effective approach to managing this situation is by treating each element as a model, allowing for easy manipulation of their states.

For instance:

new Vue({
  el: '#app',
  data: function() {
    return {
      items: [{
        label: 'Planes',
        active: false
      }, {
        label: 'Trains',
        active: false
      }, {
        label: 'Automobiles',
        active: false
      }]
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.15/vue.js"></script>

<div id="app">
  <ul>
    <li v-for="item in items" 
      @mouseenter="item.active = true"
      @mouseleave="item.active = false"
     >{{ item.label }} is {{ item.active }}</li>
  </ul>
</div>

Answer №2

Here is my solution:

thumbStyle:function(e){
            var clickedElement = e.target;
            $(clickedElement).addClass('active');
        },
        thumbStyle2:function(e){
            var clickedElement = e.target;
            $(clickedElement).removeClass('active');
        },

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

The functionality of ui-router appears to be limited when accessed from a nodejs directory, however, it functions properly on

It's strange that ui-router is not functioning as expected on my computer. The setup involves an index.html file serving as a header and test.html as an attached view. Interestingly, it works perfectly fine on Plunker. The content of index.html match ...

Guideline on extracting private keys from Windows Certificate Manager

I am currently working in a Windows environment setting. Within my organization, we act as our own certificate authority for internally-used https applications. I have obtained a certificate from our system for a private web server I created. While using ...

Transferring a method through two layers of hierarchy

I'm currently working on a fun little game project, but I've hit a roadblock when it comes to passing a particular method down to its grandchildren components. Despite finding similar discussions on this topic, none seem to address my specific qu ...

Can the useNavigation hook be used to navigate to a class component in React?

How can I use the useNavigation hook to navigate to a class component? Here is my class: export default class AzureLogin extends React.Component I want to navigate to AzureLogin from Screen1. What is the correct way to achieve this? import { useNavigati ...

Ways to update a value in a table by comparing two JSON objects

I am currently working with two JSON files containing data as shown below: JSON 1: let classes = [{ "Class": "A", "as_of": "12/31/2020", "student": [{ "raji": { "eng": ...

Configuring Braintree Client with JS v3 - encountering a null payment_method_nonce causing issues with form submission

I have successfully integrated Braintree into my Laravel 5.2 app using JS v2 client setup, but now I want to upgrade to v3. I have customized the code from the docs as follows: <form id="checkout-form" action="/checkout" method="post"> <div id= ...

Antialiasing with Three.js appears to be malfunctioning specifically on iPhone 5 and iPhone 5s devices

As a newbie to three.js, I managed to finish my project successfully. To enhance the graphics quality, I decided to enable antialiasing. renderer = new THREE.WebGLRenderer( { antialias: true } ); Unfortunately, after enabling antialiasing on iPhone 5 and ...

Uploading an image using Vue.js

Currently, I am utilizing the ElementUi uploader and facing an issue where the file details are not being sent correctly to the back-end along with my form data: Screenshots When I select an image, here is the console log: https://i.sstatic.net/StfNl.pn ...

Guarding Vue.js routes when navigating based on asynchronous authentication state requests

I have integrated Firebase for authentication in my Vue.js application. The main (main.js) Vue component handles the authentication logic as follows: created() { auth.onAuthStateChanged((user) => { this.$store.commit('user/SET_USER&apo ...

Environment variables in Node process are uninitialized

I'm currently in the process of developing my first Express application, which requires interaction with an API using a secure API key. To ensure the security of this key and any future environment variables, I have decided to store them in a .env fil ...

Alter the class following modifications to the list

https://i.stack.imgur.com/QZob0.pngIn my dual list, the data is displayed in a ul li format fetched from a JSON file. Users can move items between the two lists. However, I am facing an issue where I want to apply a property that only displays content with ...

Converting DateTime objects into JSON format for use in AJAX calls

When utilizing my AJAX method, it returns a view model that is serialized as a data structure using JavaScriptSerializer().Serialize(). Among this data are several nullable DateTime? properties. I recently discovered that these dates appear in JavaScript ...

Utilizing Node npm to access the three.js module for front-end development

My current setup involves utilizing node npm for managing dependencies such as jquery on the front end through the method outlined below. Server (successful) app.use('/jquery', express.static(__dirname + '/node_modules/jquery/dist/')) ...

Tips for dividing the rows within an array using the match() function?

Within my matrix are arrays of rows. let matrix=[['hello'],['world']]; I am looking to duplicate each row within the matrix. matrix=matrix.map(x=>String(x).repeat(2)).map(x=>x.match(new RegExp??)) The desired outcome should be [ ...

Using httpRequest to handle binary data in JavaScript

Having trouble deciphering the response of an http request that is a binary datastream representing a jpeg image, despite numerous attempts. Edit: Including the full code snippet below: xmlhttp = false; /*@cc_on@*/ /*@if (@_jscript_versio ...

The 'myCtrl' parameter is invalid as it is not recognized as a function and is currently set to undefined

I'm having trouble resolving this issue. I've implemented other controllers in the same manner that are working fine, but this specific one is throwing an error Error: ng:areq Bad Argument" "Argument 'myCtrl' is not a function, got un ...

The preflight response does not allow the access-control-request-methods request header field due to restrictions set in the access-control-allow-

I'm facing CORS issues while trying to make a POST request from my website to a remote server. Despite searching online, I couldn't find a solution that fits my specific problem. Below are the parameters for my ajax request: var params = { ...

Error Message: Stencil Launch Issue - InvalidTypeException("The parameter 'url' should be in string format, not " + the data type of url)

I have been working with stencil for quite some time now and am in the process of developing a custom theme for it. I have installed nvm, node 5.0, and npm 2. Despite deleting stencil and doing a fresh install of everything, including node modules and st ...

How can you substitute sections of a sentence with an array of strings?

I have a specific sentence that needs formatting: span class="searchmatch" Program /span, programme, programmer, or span class="searchmatch" programming /span may refer to: span class="searchmatch" Program /span management, th ...

Utilizing HTML5 to automatically refresh the page upon a change in geolocation

I have a web application built with AngularJS. One of the functionalities is activated only when the user is in close proximity to specific locations. To make this work, I can constantly refresh the page every 5 seconds and carry out calculations to dete ...