【发布时间】:2021-11-18 14:28:50
【问题描述】:
我正在尝试使用 Vue.js 在图像上创建悬停效果。我编写了以下代码,但现在当我将鼠标悬停在其中一张图像上时,文本显示在所有图像上。 我该如何解决这个问题?我只希望出现属于我悬停的图像的文本。提前感谢您的帮助。
Vue 模板:
<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>
脚本:
export default {
data: () => {
return {
showText: false,
};
},
};
【问题讨论】:
标签: javascript html vue.js vue-component