feat: FaImagePreview 组件函数式调用增加多图支持

This commit is contained in:
Hooray
2025-07-02 15:16:54 +08:00
parent ccb8bd9a18
commit 43a85f5b39
2 changed files with 39 additions and 4 deletions
+2 -1
View File
@@ -1,13 +1,14 @@
import Preview from './preview.vue'
export function useFaImagePreview() {
function open(src: string) {
function open(src: string | string[], index = 0) {
const container = document.createElement('div')
const app = createApp({
render() {
return h(Preview, {
modelValue: true,
src,
index,
})
},
})
+37 -3
View File
@@ -1,12 +1,35 @@
<script setup lang="ts">
import { Dialog, DialogContent } from './dialog'
defineProps<{
src: string
const props = defineProps<{
src: string | string[]
index?: number
}>()
const isOpen = defineModel<boolean>()
// 多图操作
const index = ref(props.index ?? 0)
const srcList = computed(() => {
if (Array.isArray(props.src)) {
return props.src
}
return [props.src]
})
function handlePrev() {
index.value = index.value - 1
if (index.value < 0) {
index.value = srcList.value.length - 1
}
}
function handleNext() {
index.value = index.value + 1
if (index.value >= srcList.value.length) {
index.value = 0
}
}
watch(index, resetImageState)
// 图片操作相关状态
const scale = ref(1)
const rotate = ref(0)
@@ -101,7 +124,7 @@ function handleAnimationEnd() {
<DialogContent class="size-full" @animation-end="handleAnimationEnd">
<div class="relative size-full flex-center" @wheel="handleWheel">
<img
:src="src"
:src="srcList[index]"
class="mx-auto max-h-full max-w-full object-contain"
:class="{
'transition-all duration-300': !isDragging,
@@ -112,6 +135,17 @@ function handleAnimationEnd() {
}"
@mousedown.prevent="handleMouseDown"
>
<template v-if="srcList.length > 1">
<div class="absolute bottom-20 left-1/2 text-sm text-muted-foreground -translate-x-1/2">
{{ index + 1 }} / {{ srcList.length }}
</div>
<FaButton variant="ghost" size="icon" class="absolute left-4 top-1/2 rounded-full bg-muted/50 -translate-y-1/2" @click="handlePrev">
<FaIcon name="i-lucide:chevron-left" class="size-6" />
</FaButton>
<FaButton variant="ghost" size="icon" class="absolute right-4 top-1/2 rounded-full bg-muted/50 -translate-y-1/2" @click="handleNext">
<FaIcon name="i-lucide:chevron-right" class="size-6" />
</FaButton>
</template>
<FaButtonGroup class="absolute bottom-4 left-1/2 scale-125 -translate-x-1/2">
<FaButton variant="outline" size="icon" class="border-none bg-muted/50" @click="handleZoomIn">
<FaIcon name="i-carbon:zoom-in" class="size-5" />