Using Vue.js to update the v-bind:style when the mouse hovers over the element

I am working with a span element that displays different background images based on certain conditions.

Here is the HTML:

  <span
      v-if="type"
      :style="styles"
    >
  </span>

In the computed properties section:

styles() {
  return {
    "background-image": `url(${require(`../assets/images/${this.type}.png`)})`,
    height: "30px",
    width: "100%",
    display: "block",
    "background-size": "contain",
    "background-repeat": "no-repeat",
    "background-position": "center",
  };
},

Additionally, I have another condition to display a different background image on mouseover.

stylesOnHover() {
  return {
    "background-image": `url(${require(`../assets/images/${this.type}-hover.png`)})`
  };
},

I attempted to add

@mouseover="stylesOnHover"
in the HTML span, but it resulted in an error :

[Vue warn]: Error in v-on handler: "TypeError: handler.apply is not a function"

Could someone guide me on how to modify the background image property on mouseover? Thank you.

Answer №1

When you return an object from your function, the style may not be properly applied to the element. One solution is to use a boolean in your state to control which styles are applied to the element, and then handle the rest in your template. Another option is to let the boolean dictate whether to merge the style in your computed styles.

Answer №2

Look no further, this is the ultimate solution you've been seeking here. Make sure to use the background-image property instead.

If you are determined to utilize mouseover effects, then stylesOnHover must be implemented as a method or computed function that returns a function. This will allow you to dynamically change the backgroundImage of event.target.style within that method/function.

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

Adjusting the filter location in React-admin

This is the common method of implementing filters in react-admin https://i.stack.imgur.com/M8yq7.png However, in my particular scenario, I require the filters to be inside each column heading. For example: https://i.stack.imgur.com/GU2Pz.png The filter ...

Incorporating a setup file into my JavaScript project

In my JavaScript project, I have both frontend and backend codes (NodeJS). Here is the folder structure for my production environment: /prod /server sourceCode1.js sourceCode2.js ... sourceCodeN.js index.js ...

Instructions on converting a SQL expression to Sequelize

Could someone assist with converting this to sequelize? CREATE TABLE "fundraiserUpdates" ( "updateId" SERIAL, "updateFundraiserId" INTEGER NOT NULL, "updateTitle" TEXT NOT NULL, "updateDescription" TEXT NOT NULL, CONSTRAINT "pk_fundraiser ...

While iterating over the key with a variable in a loop, only one property is displayed consistently instead of four different

Currently, I am working on creating an address book using JavaScript. The problem I am facing is that the properties of my objects are not providing me with the necessary information. I need 4 different properties instead of 4 loops with the same property. ...

What is the best way to display sanitized text on a webpage?

I have implemented express-validator in my project to sanitize and escape user input before saving it to my MongoDB database. However, when I try to store a URL in the database after sanitizing it, it gets stored as: https:&#x2F;&#x2F;www.youtube. ...

Tips for developing a directive that supplies values for ng-options

I have select elements with the same options throughout the entire app, but they may vary in appearance. For example, selects for a user's birthday (day, month, year). Is it possible to create a directive that can provide values or expressions for ng ...

Unable to alter the vertex color in three.js PointCloud

I'm currently facing an issue with changing the color of a vertex on a mouse click using three.js and Angular. After doing some research, I believe that setting up vertex colors is the way to go. My approach involves setting up vertex colors and then ...

Capture each touchdown and convert it to currency format

As a novice in the field of JS, I have put in a lot of effort into learning from various sources such as websites, documentation, friends, and other questions on Stack Overflow. However, despite all my efforts, I seem to be stuck at this point. $(docume ...

Is there a way to assess Python code within a document's context using JavaScript in JupyterLab?

When using Jupyter Notebooks, I can create a cell with the following JavaScript code: %%javascript IPython.notebook.kernel.execute('x = 42') After executing this code, in another cell containing Python code, the variable x will be bound to 42 a ...

Show the file name in the email signature instead of displaying the image

I'm facing a unique challenge with images in my HTML email signature. Sometimes the images are replaced by their file names on certain email hosts. Interestingly, I can't replicate this error now as the images are displaying correctly again! He ...

Determining if an element is present in a JavaScript array and returning true

I've come up with this code so far: var isMatch = viewedUserLikedUsersArray.indexOf(logged_in_user); if (isMatch >=0){ console.log('is match'); } else { console.log('no match'); } When an element is ...

jQuery Datatables have trouble accessing specific row information when the table is set to be responsive

Currently, I'm utilizing the jQuery DataTables plugin along with the responsive addon to dynamically display and hide columns based on the size of the browser window. One of the columns is labeled as Actions, which permits users to edit a record by c ...

What could be causing the TypeError in my pg-query result?

In the process of creating a node function to add a user to a PostgreSQL database, I am utilizing node.js, pg, and pg-query for communication between the application and the database. Prior to inserting a new record, I am attempting to verify that the ema ...

Steps to integrate the Save as PNG functionality in Vega using a customized menu

As I develop a data dashboard with Vega for visualizing outputs, I decided to customize the menu system by removing the actions dropdown. However, I still want to incorporate the "Save as PNG" option from the original dropdown (as shown in the image below) ...

Discover the process for breaking down JSON data into separate stages using the Express API

My API architecture is causing a problem as I try to insert it into an array called items[]. https://i.stack.imgur.com/qe802.png The setup involves a simple API built on express + mongodb. The challenge lies in figuring out how to store data from the pos ...

Tips for updating the selected date format in a Material Table component using React

In the context of using a material table in React, the columns set up are as follows: columns={[ { title: 'Name', field: 'name', type: 'string', }, ...

Adding variables and appending using jQuery

When using the append method to add HTML content dynamically to a div, I encountered an issue while trying to insert an iframe. Specifically, I needed to pass a variable's value to this frame for it to function properly. Here is the code snippet: va ...

Displaying content generated by Html.Raw in MVC4 using JavaScript and AJAX is causing displacement of content within the <a> tags in the HTML

My project includes a feature where emails sent to a specific address are parsed, with attachments saved to the network and metadata stored in a database. If the serial number of a finished good matches the one included in the email, a note is generated an ...

Align image within box using Bootstrap

My project screenshot is displayed below. The red line separates the current state from the desired outcome. I am struggling to make it fullscreen, meaning it should extend all the way to the corners. Despite trying various methods with Bootstrap 4, I have ...

Issue with Next.js middleware: Unable to locate module 'fs'

Encountering the following error in my Next.js _middleware file while attempting to initialize Firebase admin V9. Is there a solution available for this issue? ./node_modules/@google-cloud/storage/build/src/bucket.js:22:0 Module not found: Can't resol ...