VUE上想要实现点击一张完整图片的局部,显示文字
时间: 2024-05-07 17:15:13 浏览: 12
可以通过以下步骤实现:
1. 在Vue组件中,定义一个data属性,用来存储当前点击的局部图片的信息,包括图片路径、文字内容等。
2. 在模板中,使用v-for循环渲染多张图片,通过v-on:click绑定点击事件,将当前点击的图片信息保存到data属性中。
3. 在模板中,使用v-if指令判断是否有局部图片被点击,如果有,则显示一个遮罩层和对应的文字信息。
4. 在遮罩层中,使用绝对定位和transform属性,实现局部图片的放大和移动效果。
下面是一个示例代码:
```html
<template>
<div>
<div v-for="(item, index) in images" :key="index" @click="showDetail(item)">
<img :src="item.full" alt="">
</div>
<div class="mask" v-if="current">
<div class="detail">
<img :src="current.detail" alt="">
<div class="text">{{ current.text }}</div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
images: [
{
full: 'path/to/full/image.jpg',
detail: 'path/to/detail/image.jpg',
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
},
// more images here...
],
current: null
}
},
methods: {
showDetail(item) {
this.current = item
}
}
}
</script>
<style>
.mask {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 999;
}
.detail {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
max-width: 80%;
max-height: 80%;
}
.detail img {
width: 100%;
height: auto;
}
.text {
padding: 20px;
color: #fff;
font-size: 16px;
line-height: 1.5;
text-align: justify;
}
</style>
```