easybbseasybbs图片预览匡思进2025-09-202025-10-06 借助element的图片预览组件 功能 当点击点击遮罩层时, 关闭 preview hide-on-click-modal 预览图片时, 关闭页面的鼠标滚轮 stopScroll 可以在图片预览处切换查看的图片 imageList 基础封装1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556<script setup>import { ref } from 'vue'const props = defineProps({ imageList: { type: Array, },})//开始预览的const previewImageIndex = ref(null)//关闭图片预览const closeImgViewer = () => { startScroll() previewImageIndex.value = null}const show = (index) => { stopScroll() previewImageIndex.value = index}//禁止滚动const stopScroll = () => { document.body.style.overflow = "hidden"}//开始滚动const startScroll = () => { document.body.style.overflow = "auto"}defineExpose({show})</script><template> <div class="image-view"> <el-image-viewer :initial-index="previewImageIndex" hide-on-click-modal :url-list="imageList" @close="closeImgViewer" v-if="previewImageIndex != null" > </el-image-viewer> </div></template><style scoped>.image-view { el-image-viewer__mask { /* 背景透明度 */ opacity: 0.9; }}</style> 调用123456<!-- 图片预览 --> <ImageViewer ref="imageViewerRef" :imageList="previewImageList"> </ImageViewer> 123456789101112131415161718192021//图片预览const imageViewerRef = ref(null)const previewImageList = ref([])const imagePreview = () => { nextTick(() => { //等待下一次 DOM 更新循环结束后执行回调函数中的代码 const imageNodeList = document .querySelector('#detail') .querySelectorAll("img") const imageList = [] imageNodeList.forEach((item, index) => { const src = item.getAttribute("src") imageList.push(src) item.addEventListener("click", () => { imageViewerRef.value.show(index) }) }) previewImageList.value = imageList })}