Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

376
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda