Is it possible to update the image source to display a hover image using the onmouseover property when converting Vue data to a for loop?

<li v-for="user in users" :key="user.id" class="nav-list" @mouseenter="onMouseEnter" @mouseleave="onMouseLeave">
  <router-link class="nav-link" :to="nameLowerCase(user.enName)">
    <img :src="imgSrc(user)">
    <p>{{user.enName}}</p>
  </router-link>
</li>

data: function () { return { isHovering : false, } },

imgSrc(user) {
  const trimmedUrl = user.darkIconImageUrl;
  const trimmedColorUrl = user.lightIconImageUrl;    
  const trimmedBase = trim(AWS.BASEURL_ORIGINAL, '/');
  const trimmedBucket = trim(AWS.BUCKET, '/');
  if(this.isHovering) {
    return `${trimmedBase}/${trimmedBucket}/${trimmedColorUrl}`;
  } 
  return `${trimmedBase}/${trimmedBucket}/${trimmedUrl}`;
},  

When the menu items are hovered over, they switch to different images due to shared data. The attempt using onmouseover did not yield desired results. Assistance with this issue would be greatly appreciated as English is not my native language.

Answer №1

Would you be able to attempt something along these lines?

<div v-for="person in people" :key="person.id" class="grid-item" @mouseenter="onMouseEnter(person, $event)" @mouseleave="onMouseLeave(person, $event)">
  <a href="#{{person.name}}" class="link">
    <img :src="person.avatarUrl" alt="avatar">
    <p>{{person.name}}</p>
  </a>
</div>

data: function(){
    return {
        avatarUrls: []
    }
},

methods: {
    onMouseEnter(person, event){
        this.setAvatarUrl(person, true);
    },
    onMouseLeave(person, event){
        this.setAvatarUrl(person, false);
    },
    setAvatarUrl(person, isHover) {
      const normalUrl = person.defaultAvatarUrl;
      const hoverUrl = person.hoverAvatarUrl;
      const trimmedBase = trim(AWS.IMAGE_BASE_URL, '/');
      const trimmedBucket = trim(AWS.BUCKET_NAME, '/');
      
      if(isHover) {
        this.avatarUrls[person.id] = `${trimmedBase}/${trimmedBucket}/${hoverUrl}`;
      } else {
        this.avatarUrls[person.id] = `${trimmedBase}/${trimmedBucket}/${normalUrl}`;
      }
    }, 
}

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

Sharing data between a Javascript applet and a webpage: strategies for sending results from the applet back to

When it comes to calling a method in an applet from JavaScript, I am facing a challenge. Both the applet and the JavaScript code are running on the same webpage. I am aware of how to call applet methods from JavaScript and vice versa using techniques like ...

Support for both Fetch API and XMLHttpRequest across browsers is imperative for seamless data retrieval and

Currently, I am diving into the world of ajax programming techniques. However, I recently discovered that the XMLHttpRequest is deprecated and now I must transition to using the Fetch API. The catch is, according to MDN, the Fetch API is still considered e ...

Cannot use React's setState function

This element represents the main container: modifyData(){ this.setState({ modified: true }).bind(this) } I am passing it to a sub-element: <Profile updateData={this.modifyData} auth={this.props.auth} details={profile}/> The sub ...

What is the purpose of the 'onClassExtended' function in Extjs 6 for class definition?

Ext.define('Algorithm.data.Simulated', { needs: [ //.... ], onClassExtended: function(obj, info) { // .... } }) I came across this code snippet but couldn't locate any official documentation for it on Sencha ...

Ways to retrieve JSON data using Angular JS

I'm currently working on populating my table with data. The URL returns JSON data, but I'm struggling with how to retrieve it using AngularJS. Here is my services.js: angular.module('OrganisatieApp.services', []) .factory('organi ...

Having trouble with asynchronous JSON calls in JavaScript when setting async to false

I'm having trouble figuring out what I'm doing wrong in this scenario. The issue is that I can't seem to reassign the variable poster_path with the fetched poster-path from the JSON call. What's puzzling to me is that I even tried setti ...

SwiperJS continuously applies additional right margin to every slide

My Swiper carousel seems to be adding an unnecessary 5px margin-right to each slide, resulting in the cards not fitting into one frame. https://i.sstatic.net/fwn5G.png I attempted to fix this by setting the margin-right to 0px for the .swiper-slide class ...

The iOS platform is experiencing issues with displaying the entire page, making it difficult to

Struggling with my website for Freecodecamp. Works fine on desktop, but iOs is a nightmare. My contact page isn't showing at all, and 75% of the bottom is cut off. Even worse, I can't click on what's visible. Help needed! Only tested on iPho ...

Mastering the art of utilizing callbacks in AngularJS for consuming an API

Having trouble handling data from an API and structuring it effectively before passing it to the controller. I've created a factory that retrieves user data from the API, but the provideAllUserData function is causing issues. Below is my services.js: ...

How to organize the cpuinfo output in BASH

I'm currently working on a script that collects information from our servers and outputs the results to an HTML file. Everything is going smoothly up to a certain point. I've run into an issue when trying to grab the cpuinfo. While it does extrac ...

How can I ensure an element fills the width of its container without being pushed out by margins?

Is there a way to make an element adjust its width based on the container size without overflowing it? body { -moz-osx-font-smoothing: grayscale; font-family:Verdana; font-weight:normal; font-size:12px; } #BorderContainer2085 { position:absolute; top: ...

When trying to upload a file through input using WebDriver, the process gets stuck once the sendKeys

WebElement uploadInput = browser.findElementByXPath("[correct_identifier]"); uploadInput.sendKeys(elementPath); The script successfully initiates the file upload process, however, the custom JavaScript loading screen persists and fails to disappear as exp ...

Implementing the insertion of a <div> element within an input field using jQuery

<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></scr ...

JavaScript-enabled tests encountering issues (Bootstrap 3, Rails 4, Travis CI)

I'm encountering a strange error that only shows up in my CI environment. This error doesn't occur in development, production, or local test environments. ActionController::RoutingError: No route matches [GET] "/fonts/bootstrap/glyphicons-halfli ...

CSS tip: A workaround for hiding overflow without affecting 3D transforms is to use "overflow: hidden" in a different way

I'm currently working on creating a horizontal section with a parallax effect. My goal is to have an image in the background that scrolls at a different speed than the rest of the page. My challenge lies in containing the parallax element within its ...

Assigning path handlers to external modules in NodeJS using Express

I've been trying to minimize the complexity in my routes.js file. Check it out: module.exports = function(app, db) { app.get('/', function(req, res) { res.render('index') }); app.get('/contact-us', function(req, re ...

Adjust the color of the font icon within a button when hovering over it

On the bottom of my website (still in development) , there are 3 buttons with an arrow icon preceding the text. The arrow is created using an icon font and I would like it to change color (along with the button text) when hovered over. Any suggestions on ...

Hold off on running the function until it has fully executed before moving on to other code execution

I have spent the entire day trying to solve this problem, but it seems that I am unable to do so. Here is the code I have been working on: function mailcheckajax(email){ //var add = $('#mail').val(); //window.location = "index.php?addre ...

What is preventing the expected outcome of setting this as a background image in NextJS 13 with Tailwind CSS?

Can someone help me figure out how to achieve the desired effect for this component? import React from "react"; import Image from "next/image"; import heroBackground from "../public/hero-background.png"; const Hero = () => ...

Using multiple columns for ordering in bookshelfjs

How can I implement multiple orderBy in BookshelfJs? In the API, there may be various sort options available, such as example.com/users?sort=-name,status, and it needs to be dynamic rather than hard coded. The answer provided in the link below seems suit ...