vue实现弹照片
Vue 实现图片弹窗展示
基础弹窗实现
使用 Vue 的 v-if 或 v-show 控制弹窗显示状态,结合 CSS 实现遮罩层效果
<template>
<div>
<img
src="your-image.jpg"
@click="showModal = true"
class="thumbnail"
>
<div v-if="showModal" class="modal">
<span class="close" @click="showModal = false">×</span>
<img class="modal-content" :src="selectedImage">
</div>
</div>
</template>
<script>
export default {
data() {
return {
showModal: false,
selectedImage: 'your-image.jpg'
}
}
}
</script>
<style>
.thumbnail {
cursor: pointer;
width: 200px;
height: auto;
}
.modal {
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.9);
}
.modal-content {
margin: auto;
display: block;
max-width: 80%;
max-height: 80%;
position: relative;
top: 50%;
transform: translateY(-50%);
}
.close {
position: absolute;
top: 15px;
right: 35px;
color: white;
font-size: 40px;
font-weight: bold;
cursor: pointer;
}
</style>
使用第三方组件库
Element UI 实现方式:
<template>
<div>
<el-image
style="width: 200px; height: 200px"
:src="url"
:preview-src-list="[url]"
>
</el-image>
</div>
</template>
<script>
export default {
data() {
return {
url: 'your-image.jpg'
}
}
}
</script>
图片画廊实现
实现多图片预览功能:
<template>
<div>
<div v-for="(img, index) in images" :key="index">
<img
:src="img.thumbnail"
@click="openGallery(index)"
class="gallery-thumb"
>
</div>
<div v-if="galleryVisible" class="gallery-modal">
<button @click="prevImage">❮</button>
<img :src="images[currentIndex].fullSize">
<button @click="nextImage">❯</button>
<span @click="closeGallery" class="close">×</span>
</div>
</div>
</template>
<script>
export default {
data() {
return {
images: [
{ thumbnail: 'thumb1.jpg', fullSize: 'full1.jpg' },
{ thumbnail: 'thumb2.jpg', fullSize: 'full2.jpg' }
],
galleryVisible: false,
currentIndex: 0
}
},
methods: {
openGallery(index) {
this.currentIndex = index
this.galleryVisible = true
},
closeGallery() {
this.galleryVisible = false
},
prevImage() {
this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length
},
nextImage() {
this.currentIndex = (this.currentIndex + 1) % this.images.length
}
}
}
</script>
动画效果增强
添加过渡动画使弹窗更平滑:

.fade-enter-active, .fade-leave-active {
transition: opacity .5s;
}
.fade-enter, .fade-leave-to {
opacity: 0;
}
.modal-content {
animation: zoomIn 0.3s;
}
@keyframes zoomIn {
from { transform: scale(0.5) translateY(-50%); }
to { transform: scale(1) translateY(-50%); }
}






