Activate the active class on the icon when hovering over a dynamically generated span using Vue

Is it possible to activate multiple music notes when hovering over one? For example, if I hover over music note 2, both note 1 and note 2 should get active classes. And if I hover over note 3, then all three spans/icons should become active.

I have successfully implemented this feature with a click event. How can I achieve the same functionality with a hover event?

This is the code snippet I am currently using:

 <template>
    <div class="track-rating">
       <span :key="note" v-for="note in maxNotes" :class="{ 'active': note <= notes }" @click="rate(note)" class="material-icons mr-1">
        audiotrack
       </span>
    </div>
  </template>
  <script>
    export default {
      name: "Rating",
      props: {
        rating: {
            type: Number,
            required: true
        },
        maxNotes: {
            type: Number,
            default: 3
        },
        hasCounter: {
            type: Boolean,
            default: true
        }
      },
      data() {
        return {
            notes: this.rating
        };
      },
      methods: {
        rate(note) {
            if (typeof note === 'number' && note <= this.maxNotes && note >= 0)
                this.notes = this.notes === note ? note - 1 : note
        }
      }
   };
 </script>

================

<template>
   <div>
     <Rating :rating="0"/>
   </div>
</template>
<script>
   import Rating from '../Rating';
   export default {
     name: "Test",

     components: {
        Rating
     },
    
   };
 </script>

Answer №1

One way to enhance this functionality is by storing the currently hovered note and utilizing it to apply the active class as well.

<template>
    <div class="track-rating">
       <span 
              v-for="note in maxNotes" 
             :class="{ 'active': note <= notes || note <= hoveredNote }" 
             @mouseover="hoveredNote = note"  
             @mouseleave="hoveredNote = false" 
             @click="rate(note)" class="material-icons mr-1">
        audiotrack
       </span>
    </div>
  </template>
  <script>
    export default {
      name: "Rating",
      props: {
        rating: {
            type: Number,
            required: true
        },
        maxNotes: {
            type: Number,
            default: 3
        },
        hasCounter: {
            type: Boolean,
            default: true
        }
      },
      data() {
        return {
            notes: this.rating,
            hoveredNote: false,
        };
      },
      methods: {
        rate(note) {
            if (typeof note === 'number' && note <= this.maxNotes && note >= 0)
                this.notes = this.notes === note ? note - 1 : note
        }
      }
   };
 </script>

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

Cypress: Uncovering the method invoked by a button click

I'm currently utilizing Vue3 with Vite and Cypress. My Vue3 component utilizes the script setup SFC syntax. Below is the code snippet for my component: <template> <div> <button data-cy="testBtn" @click="btnClick()&q ...

Explore nested JSON data by clicking on it

Scenario I am currently working with complex JSON data that requires filtering and reorganizing. The data is structured as a hierarchical collection of projects, each containing task lists which, in turn, contain tasks. In essence, the JSON tree follows ...

The slider feature is malfunctioning on Internet Explorer version 8

Hello everyone, I am facing an issue with my website located at: http://210.48.94.218/~printabl/ When viewed in IE 8, the slider is not functioning properly (it is showing collapsed between the menu and product images section) The theme used for my site ...

Having trouble with the button's functionality in Internet Explorer?

Recently, I encountered an issue with a "dropdown button" that I created using only CSS and HTML. Surprisingly, it works flawlessly in Chrome and FireFox, but seems to have some glitches when tested on Internet Explorer (IE). The "New Invoice" button fails ...

What methods can I use to prevent a number from becoming negative?

I am currently developing a game where players collect resources, spend them to create more resources, and engage in various activities. However, I'm facing an issue where the resource count goes into negative numbers when too much of a certain item i ...

What is the best way to integrate content from the tiptap text editor into a v-model?

Trying to directly bind this.newTutorial.content to editor.content, but no success so far. Console output: https://i.sstatic.net/BNRR4.png Code snippet: <style scoped> img.preview { width:200px; } .v-btn { height: 50px !important; min-wi ...

A pale tooltip with a light arrow appearing against a soft white backdrop

Can someone help me figure out how to display a white tooltip with a white arrow? I've tried to implement it using the code below, but the white color is not visible against the background. Any suggestions on making it stand out? https://i.sstatic.ne ...

Tips for preventing page breaks (when printing or saving as a PDF) in lengthy HTML tables

Here is the link to a single HTML file (including style and scripts): FQ.html The problem I'm facing can be seen in this image: https://i.sstatic.net/Nr4BZ.png I've tried several solutions, the latest of which involves the following CSS... @me ...

How to send dynamic values to a computed function in Vue.js

Trying to pass an id with the v-model in order to dynamically filter a table without hardcoding names into the function. The goal is for the function to be able to filter both select boxes. The data is referred to as guides, containing attributes such as ...

Enhance Vue table field presentation

I am currently working on converting a Unix timestamp into a user-friendly date format, but I have encountered an issue. My desired implementation looks like this: <Column field="createdAt" header="Created at">{{ new Date(this).to ...

Modify the color or background color of a disabled Material UI checkbox

The disabled unchecked checkbox appears too subtle to me, and I would like to enhance it by giving it a grey background and changing the cursor style to not-allowed. I've been trying to implement these styles on the checkbox using the makeStyles, but ...

Increase the options available in the dropdown menu by adding more selected items, without removing any already selected

I came across this code on the internet http://jsfiddle.net/bvotcode/owhq5jat/ When I select a new item, the old item is replaced by the new item. How can I add more items without replacing them when I click "dropdown list"? Thank you <select id=&qu ...

The vue-croppa component is showing unusual behavior, with an error message stating "Failed to mount component: template or render function not

I recently tried utilizing vue-croppa for image cropping in my project. I installed it using the npm package manager with the command: npm install --save vue-croppa However, when attempting to implement it as usual: import Croppa from 'vue-croppa&a ...

Ways to expand the tooltip width in Bootstrap-Vue

Is there a way to make the tooltip wider in Bootstrap Vue? I have a long statement to display in the tooltip, but it is only showing three words per row, resulting in a taller tooltip with less width. <div> <span id="disabled-wrapper" class=" ...

What is causing the width discrepancy in my header section on mobile devices?

Help needed with website responsiveness issue! The site works fine on most screen sizes, but when it reaches around 414px in width, the intro section becomes too wide for the screen. Any ideas on what could be causing this problem? html: <nav id="m ...

The navigation bar is not extending to fill the entire width of the screen

I recently created a navigation bar using the nav tag and applied CSS to set the width, border, and padding. However, I noticed that there are some empty pixels on each side of the navigation bar that I can't seem to fill up. Here is the HTML code for ...

The compatibility issue between Express Session and Vue.js using Axios is causing issues

The initialization of the session in express.js also utilizes passport.js for local authentication, which is functioning properly. However, there seems to be an issue with the session/cookie not working. app.use(serveStatic(__dirname + '../../dist&a ...

Using jQuery UI to style div elements with dynamic titles

Is it possible to style a standard div container using the same styling as jQuery dialogs? I am looking to create a div panel with text content but with the title bar styling similar to that of jQuery UI dialog boxes. Appreciate your assistance in advance ...

Is it possible to access an external website by clicking on a link within a DIV element?

I have a basic website set up like this sample.php <html> <head></head> <body> <a id="myLink" href="http://sample.com">Click!</a> </body> </html> I displayed this website within a DIV-Container on a ...

What strategies can I employ to prevent my div tags from shifting positions relative to each other?

I spent time formatting a div tag for my main content and then placed that div inside another wrapper div. However, when I tried to add my logo to the site, everything shifted. It appeared as though the div containing the logo had pushed the wrapper down. ...