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

When employing `queryParams` in Angular routing, the URL will automatically replace `%` with `%25`

When trying to use queryParams for navigation in Angular routing, I encountered an issue. <a routerLink='/master' [queryParams]="{query:'%US',mode:'text'}"><li (click)="search()">Search</li></a> The d ...

Issue with unit testing in Firestore: Anticipated data type was supposed to be 'DocumentReference', however, what was received was a unique Firestore object

I am trying to execute unit tests for Firestore. Below is the code snippet I am using: import { getDoc, setDoc } from "@firebase/firestore"; import { assertFails, assertSucceeds, initializeTestEnvironment, RulesTestEnvironment, } from &qu ...

Performing simultaneous AJAX requests in Javascript and jQuery

function makeCall(file, handlerFile, sendMethod, formData) { //console.log(instance.files); $.ajax({ url: handlerFile, type: sendMethod, xhr: function() { // Custom XMLHttpRequest var xhr = $.ajaxSettings.xhr() ...

Using Javascript to display an element when scrolling within a specific container and each item of an array reaches the top

I'm just starting out with JavaScript and I'm attempting to create a scrollable div that includes items from an array. As the user scrolls and each item reaches the top, I want a hidden element to be displayed. Here's what I have so far: ...

Utilize a function in module.exports that calls for a variable within the module

I am currently working on designing my modules in such a way that they don't need to be required multiple times. In my approach, I am passing arguments from the main app.js file to all of my modules. One of the modules I have is for user login and it ...

Error with JavaScript callback functions

After creating a POST route, I encountered unexpected behavior in the code below. The message variable does not display the expected output. app.post("/", function (req, res, error) { var message = ""; tableSvc.createTable("tableName", function (error ...

Executing a node.js function within an Angular 2 application

Currently, I am running an Angular2 application on http://localhost:4200/. Within this app, I am attempting to call a function located in a separate node.js application that is running on http://localhost:3000/. This is the function call from my Angular2 ...

Ionic (Angular) experiencing crashes due to numerous HTTP requests being made

My template contains a list of items <ion-list class="ion-text-center"> <div *ngIf="farms$ | async as farmData"> <ion-item (click)="selectFarm(farm)" *ngFor="let farm of farmData" detail=&quo ...

Scenario-specific blueprints

I'm currently facing a challenge in incorporating logic into a dustjs template, and I find it challenging to integrate all the components seamlessly. Here is an example of the JSON data I have: { "names": [{ "name": "User 1", "is ...

Using string literals in Vue and HTML classes

When a user attempts to log in with the wrong password, I want multiple alert popups to appear on the UI instead of just one. This will help the user understand if they are still entering the incorrect password. To achieve this, I decided to use a counter ...

Unintentional GET request triggered by Axios baseURL

I have encountered a strange issue where defining axios.defaults.baseURL = baseUrl; results in an unexpected GET request right after initializing my Vue app. Any assistance would be greatly appreciated! Below are images showing the code and network reques ...

Is it advisable to incorporate both Jquery and AngularJs into progressive web applications?

Currently, I am in the process of constructing a progressive test application. So far, all the tutorials I have come across utilize native JavaScript. My inquiry is whether it is possible to leverage Jquery for streamlined code writing and implement an MVC ...

Evolution of ReactJS state over time

When working with React, I wanted to increment a state variable called progressValue by 0.1 every 500 ms until it reaches 100. Here's what I initially tried: const [progressValue, setProgressValue] = React.useState<number>(0) const tick ...

Creating users or custom roles in MongoDB on a NodeJS server is not currently possible

I have been attempting to directly create users on my database through our Express server, utilizing MongoDB 3.4 for the backend. Below is the current code snippet from the server: const express = require('express'); const bodyParser = require(& ...

The Vue-cli webpack development server refuses to overlook certain selected files

I am attempting to exclude all *.html files so that the webpack devserver does not reload when those files change. Here is what my configuration looks like: const path = require('path'); module.exports = { pages: { index: ...

Newbie's guide: Saving information in Ruby on Rails

In my Rails 4 application, I am looking to save questions and answers either in a document or database. Once stored, I want to showcase them on a specific webpage for users to respond. For instance, I envision having a page titled /questions where a quest ...

AngularJS creates a new window in your application

I have created two html pages named web.html and addroute.html. Both pages are built with bootstrap. The angularjs script includes two controllers: webctrl and addctrl. In web.html, there is a button that, when clicked, opens the addroute.html page. I am ...

Tips for Properly Positioning Floating Pop-Up Messages Using CSS and jQuery

I was experimenting with adding a button that triggers a popup message to my website. I followed a coding tutorial using jQuery and CSS, which looks like this: Javascript : !function (v) { v.fn.floatingWhatsApp = function (e) {...} }(jQuery); CSS : .fl ...

Error: The function bind is not supported on instance[method] in the AdonisJs framework

I am currently integrating the MQTT-adonis module adonis-mqtt response on Git here in my adonis-js application. However, when trying to serve it, an exception is thrown. TypeError: instance[method].bind is not a function Could anyone provide guidance o ...

Pass information submitted through a JavaScript prompt to an expressjs endpoint

I'm currently facing a challenge in extracting the value from my prompt in order to modify a category using a JavaScript function. Typically, I would rely on a form to pass variables to the request.body, but that's not an option here. This is wh ...