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,
};
},
};
CodePudding user response:
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,
};
},
};
CodePudding user response:
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"/>