Element Plus 自定义图片预览组件

155 字
1 分钟
Element Plus 自定义图片预览组件

创建一个图片预览组件#

<script setup lang="ts">
import { ref, watchEffect } from "vue";
const props = defineProps<{
imageList?: string[];
}>();
const visible = ref(false);
const list = ref<string[]>([]);
const currentIndex = ref<number>(0);
watchEffect(() => {
const { imageList } = props;
if (!imageList?.length) return;
list.value = [...imageList];
});
function open(data: { previewIndex: number }) {
const { previewIndex } = data;
currentIndex.value = previewIndex;
visible.value = true;
}
const emit = defineEmits<{
(e: "close"): void;
}>();
function close() {
visible.value = false;
emit("close");
}
// 切换图片时触发
function onSwitch(index: number) {
currentIndex.value = index;
}
defineExpose({
open,
close,
});
</script>
<template>
<div id="image-preview" v-if="visible">
<el-image-viewer
:url-list="list"
@close="close"
@switch="onSwitch"
:initial-index="currentIndex"
show-progress
>
</el-image-viewer>
</div>
</template>
<style scoped lang="scss">
#image-preview {
position: relative;
:deep(.el-image-viewer__progress) {
position: absolute;
top: 0;
height: 40px;
width: 100%;
background: rgba(0, 0, 0, 0.1);
border-radius: 0;
font-weight: 400;
}
}
</style>

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!

打赏
Element Plus 自定义图片预览组件
https://blog.ddlyt.top/posts/2025/0516/
作者
兜兜里有糖
发布于
2025-05-16
许可协议
CC BY-NC-ND 4.0

评论区

Profile Image of the Author
兜兜里有糖
越努力,越幸运。
公告
欢迎来到 DBlog,这里记录前端开发、技术学习和一些实用工具。
分类
标签
最新动态
站点统计
文章
21
分类
2
标签
18
总字数
15,966
运行时长
0 天
最后活动
0 天前
访问次数
--
站点信息
构建平台
EdgeOne Pages
博客版本
Firefly v2.0.0
文章许可
CC BY-NC-ND 4.0
文章目录