Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

374
Views
Why every text shows on img hover when using v-if, v-else, @mouseover and @mouseleave in Vue.js?

I am trying to create a hover effect on images with Vue.js. I wrote the following code, but now as I hover over one of the images, the text shows up over all the images. How can I resolve this problem? I want only the text that belongs to the image I hovered over to appear. Thank you in advance for your help.

Vue template:


<template>
<div class="row partner-body-row">
        <div class="col">
          <div
            class="img-wrapper"
            @mouseover="showText = true"
            @mouseleave="showText = false"
          >
            <img
              class="hover-img"
              src="img/img-1"
              alt="img-1"
            />
            <span v-if="showText">Text 1</span>
          </div>
        </div>
<div class="col">
          <div
            class="img-wrapper"
            @mouseover="showText = true"
            @mouseleave="showText = false"
          >
            <img
              class="hover-img"
              src="img/img-2"
              alt="img-2"
            />
            <span v-if="showText">Text 2</span>
          </div>
        </div>
        <div class="col">
          <div
            class="img-wrapper"
            @mouseover="showText = true"
            @mouseleave="showText = false"
          >
            <img
              class="hover-img"
              src="img/img-3"
              alt="img-3"
            />
            <span v-if="showText">Text 3</span>
          </div>
        </div>
        
     <div class="col">
          <div
            class="img-wrapper"
            @mouseover="showText = true"
            @mouseleave="showText = false">
            <img
              class="hover-img"
              src="img/img-4"
              alt="img-4"
            />
            <span v-if="showText">Text 4</span>
          </div>
        </div>
      </div>
</template>

Script:

export default {
  data: () => {
    return {
      showText: false,
    };
  },
};
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

all the conditions are tied to the same variable, have that variable hold each image's number rather than just true/false.

and ideally this should be done in CSS using :hover

<template>
<div class="row partner-body-row">
        <div class="col">
          <div
            class="img-wrapper"
            @mouseover="showText = 1"
            @mouseleave="showText = 0"
          >
            <img
              class="hover-img"
              src="img/img-1"
              alt="img-1"
            />
            <span v-if="showText === 1">Text 1</span>
          </div>
        </div>
<div class="col">
          <div
            class="img-wrapper"
            @mouseover="showText = 2"
            @mouseleave="showText = 0"
          >
            <img
              class="hover-img"
              src="img/img-2"
              alt="img-2"
            />
            <span v-if="showText === 2">Text 2</span>
          </div>
        </div>
        <div class="col">
          <div
            class="img-wrapper"
            @mouseover="showText = 3"
            @mouseleave="showText = 0"
          >
            <img
              class="hover-img"
              src="img/img-3"
              alt="img-3"
            />
            <span v-if="showText === 3">Text 3</span>
          </div>
        </div>
        
     <div class="col">
          <div
            class="img-wrapper"
            @mouseover="showText = 4"
            @mouseleave="showText = 0">
            <img
              class="hover-img"
              src="img/img-4"
              alt="img-4"
            />
            <span v-if="showText === 4">Text 4</span>
          </div>
        </div>
      </div>
</template>

export default {
  data: () => {
    return {
      showText: 0,
    };
  },
};
about 4 years ago · Juan Pablo Isaza Report

0

You could just create a component for each Image with tooltip, something like:

Vue.config.productionTip = false;

const Img = {
  name: 'Img',
  props: ['src'],
  data() {
    return { showText: false }
  },
  template: '<div><img @mouseover="showText = true" @mouseleave="showText = false":src="src" /><span v-if="showText">Tooltip</span></div>',
};

const App = new Vue({
  el: '#root',
  components: { Img },
  template: '<div><Img src="https://via.placeholder.com/150"/><Img src="https://via.placeholder.com/150"/></div>',
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="root"/>

about 4 years ago · Juan Pablo Isaza Report

0

if it's only to show the text on hover. You don't need to use v-model. in this case you would use css.

You could remove all the v-if's from the <span> and add this css

  .image-container span {
     display: 'none';
  }

  .image-container:hover span {
    display: 'block';
  }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!