Utilizing Vue JS to showcase a pop-up block when hovering over a specific image

There are four images displayed, and when you hover over each one, different content appears. For example, hovering over the first image reveals a green block, while hovering over the second image reveals a blue block. However, the current logic in place is quite complex and difficult to follow.

If you want to see the existing logic and code for this functionality, you can check out this link. I'm curious to know if there's a way to simplify and optimize this code to make it more readable and straightforward.

<template>
  <div>
    <div id="girls_gallery">
      <div class="girls_gallery_content">
        <div>
          <div class="g_gallery_title_container">
            <h1>Hover Image</h1>
          </div>
          <div class="girls_list">
            <div class="girls_container">
              <img
                style="width: 200px; height: 200px"
                @mouseover="mouseOver1"
                @mouseout="mouseout"
                src="https://www.gettyimages.com/gi-resources/images/500px/983794168.jpg"
                alt="Snow"
              />
            </div>

            <div class="girls_container">
              <img
                style="width: 200px; height: 200px"
                @mouseover="mouseOver2"
                @mouseout="mouseout"
                src="https://www.gettyimages.com/gi-resources/images/500px/983794168.jpg"
                alt="Snow"
              />
            </div>

            <div class="girls_container">
              <img
                style="width: 200px; height: 200px"
                @mouseover="mouseOver3"
                @mouseout="mouseout"
                src="https://www.gettyimages.com/gi-resources/images/500px/983794168.jpg"
                alt="Snow"
              />
            </div>

            <div class="girls_container">
              <img
                style="width: 200px; height: 200px"
                @mouseover="mouseOver4"
                @mouseout="mouseout"
                src="https://www.gettyimages.com/gi-resources/images/500px/983794168.jpg"
                alt="Snow"
              />
            </div>
          </div>
        </div>
      </div>
    </div>
    <div style="margin-top: 50px">
      <div
        style="width: 200px; height: 200px; background: red"
        v-show="img1"
        key="img1"
      ></div>
      <div
        style="width: 200px; height: 200px; background: green"
        v-show="img2"
        key="img1"
      ></div>
      <div
        style="width: 200px; height: 200px; background: blue"
        v-show="img3"
        key="img1"
      ></div>
      <div
        style="width: 200px; height: 200px; background: orange"
        v-show="img4"
        key="img1"
      ></div>
    </div>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",

  data() {
    return {
      img1: false,
      img2: false,
      img3: false,
      img4: false,
    };
  },

  methods: {
    mouseOver1: function () {
      this.img1 = true;
    },
    mouseOver2: function () {
      this.img2 = true;
    },
    mouseOver3: function () {
      this.img3 = true;
    },
    mouseOver4: function () {
      this.img4 = true;
    },
    mouseout: function () {
      this.img1 = false;
      this.img2 = false;
      this.img3 = false;
      this.img4 = false;
    },
  },
};
</script>

Answer №1

It appears that the concept of how Vue is intended to function hasn't quite sunk in yet. Take a look at this example for clarification: https://codesandbox.io/s/wandering-dream-237l0?file=/src/components/HelloWorld.vue

The basic idea is to render your component content based on the data within the component. This involves creating a data object that contains the necessary information for your logic.

Answer №2

To enhance the design, I suggest adding position: relative to the girls_container class and relocating the color block inside it.

Additionally, include the following CSS properties for the color blocks:

z-index: 9999;
position: absolute;
top: 0;
left: 0;

It's also recommended to transfer the event handlers @mouseover and @mouseout to the girls_container element.

<div class="girls_list">
  <div
    class="girls_container"
    style="position: relative"
    @mouseover="mouseOver1"
    @mouseout="mouseout"
  >
    <img
      ref="img1"
      style="width: 200px; height: 200px"
      src="https://www.gettyimages.com/gi-resources/images/500px/983794168.jpg"
      alt="Snow"
    />
    <div
      style="
        width: 200px;
        height: 200px;
        background: red;
        z-index: 9999;
        position: absolute;
        top: 0;
        left: 0;
      "
      v-show="img1"
      key="img1"
      ref="img-block1"
    ></div>
  </div>

Check out the example on Code SandBox

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

How to extract a one-of-a-kind identification number from the browser using just JavaScript in a basic HTML file?

Is there a way to obtain a distinct identification number from a browser without resorting to techniques such as generating and storing a unique number in cookies or local storage, and without utilizing a server-side language? ...

Using API calls to update component state in React-Redux

I am currently working on setting up a React application where clicking on a map marker in one component triggers the re-rendering of another component on the page. This new component should display data retrieved from a database and update the URL accordi ...

Occasional TypeError when receiving JSONP response using jQuery .ajax()

Occasionally, I encounter an error message stating Uncaught TypeError: undefined is not a function when processing the JSONP response from my jQuery .ajax() call. The JSON is returned successfully, but sometimes this error occurs during the reading process ...

Tips for creating a navigation bar item that displays a component depending on its active state

Trying to enhance the modularity of my code but facing difficulties. I have a tab bar and I want to render a specific component based on the clicked nav/tab item. Struggling with passing props properly, as the current code only recognizes the children valu ...

What is the process for including an additional button in the DateTimePicker feature of material UI?

I'm currently utilizing DateTimePicker in my React application. I wish to incorporate a Clear button positioned to the left of the Cancel Button. import { MuiPickersUtilsProvider, DateTimePicker } from "@material-ui/pickers"; import DateFnsUtils fro ...

Issue with useEffect EventListener in REACT HOOKS

Recently, I attempted to create a simple Snake-Game using REACT. Everything was going smoothly until I encountered an issue with using useEffect for moving the "snake" when a keydown event is triggered. The challenge arose when trying to implement moveSnak ...

Sending a Javascript object to PHP for decoding as JSON can be accomplished by

After searching through numerous similar posts without success, I am still struggling to get this dynamic 2-dimensional JavaScript object to work. My goal is to pass it to PHP in order to insert it into a MySQL table. Utilizing an Ajax post seems to be the ...

What is the method for determining the numerical worth of the px containers?

https://i.stack.imgur.com/0K2TD.png Total width of the bar is set to 500px, with the red box at a width of 150px, the yellow box at 200px, and the green box at 50px. CSS Styles: .box { float:left; width:150px; box-shadow:3px 3p ...

Top method for transferring data between React components post retrieval from Axios Call

I am currently utilizing React JS in an application structured like the following diagram: This particular application fetches data from a Rest API (Node express) using Axios. The challenge I am facing is determining the most effective method for storing ...

execute numerous queries simultaneously

I have a task of creating a bridge (script) that connects two databases, Mongo and Oracle. First, I execute three find queries on my MONGO database from three different collections: Collection 1 = [{ name: 'Toto', from: 'Momo', ...

Is it possible to delete an element from an HTML form?

I'm facing an issue with removing an element from my HTML form during the loading process. The specific element in question is as follows: <input type="hidden" name="testToken" value="1000ad"></input> Here's what I've tried so ...

Is there a way to make my code on Google Sheets work across multiple tabs?

My Google Sheets code successfully pulls information from the main tab into my CRM Software every time someone fills out a form online. However, I'm struggling to get the script to run for multiple tabs on the same spreadsheet. I've tried a few s ...

Having trouble establishing a two-way binding between a Vue.js component and its model in the latest version 3.4.21

I am currently in the process of learning Vue.js, but I am facing issues with the code provided in the official documentation. Despite following the instructions, I keep encountering an error message from Vue stating: Property "PROPERTY_NAME" was ...

Mapping geographic coordinates with a null projection using D3

With d3.geo.path having a null projection due to TopoJSON already being projected, it can be displayed without any additional transformation. My goal is to plot data in the format of [longitude, latitude] on a map. Here is a simplified version of my code: ...

Utilize Javascript to extract and showcase JSON data fetched from a RESTful API

I'm attempting to use JavaScript to pull JSON data from a REST API and display it on a webpage. The REST call is functioning correctly in the Firefox console. function gethosts() { var xhttp = new XMLHttpRequest(); xhttp.open("GET", "https://10 ...

What is the best way to activate a function from a modal component without having the function embedded in Angular 14?

I've recently been putting together an e-commerce application using Angular 14. Currently, I'm tackling a form that must only be submitted once the user accepts the "Terms & Conditions" shown in a modal popup. The FormComponent and ModalCompone ...

Is there a way to connect and interact with a different ng-controller's ng-model within a separate ng-controller?

Is it possible to access the ng-model from another ng-controller and if so, how can it be done? In this scenario, I am using two controllers. The first controller has a model called mddl1, while the second controller does not have any other model. However, ...

Troubleshooting: Next JS 13/14 - Server component failing to update data upon revisiting without requiring a refresh

After attempting to retrieve the most recent liked items from a server component in next, I noticed that the data only displays when manually refreshing the page. Even when I navigate away and return, the data is not refetched automatically - however, usin ...

Encountering issues with Jest Setup in Next.js as it appears to unexpectedly include a React import in index.test.js

Hey there, I've been pondering over this issue for the past few days. It appears to be a common error with multiple solutions. I'm facing the dreaded: Jest encountered an unexpected token /__tests__/index.test.js:16 import React from "r ...

JQuery jqx validation is experiencing some issues

Utilizing jquery plugins and widgets from jqx for basic form validation in my ruby-on-rails application has proven to be very helpful. Here is a simple example of an HTML form: <form id="newForm"> <input type="text" id="name"/> < ...