<template>
<div class="image-preview-demo">
<div class="image-preview-demo__images">
<button v-for="(item, index) in images" :key="item.url" type="button"
:aria-label="'预览' + item.name" :disabled="opened" @click="open(index)">
<img :src="item.thumbUrl" :alt="item.name" />
</button>
</div>
<p aria-live="polite">{{ message }}</p>
</div>
</template>
<script setup>
import { getCurrentInstance, ref } from 'vue'
import { $ImagePreview } from '@smallwei/avue'
const { appContext } = getCurrentInstance()
const preview = $ImagePreview(appContext)
const opened = ref(false)
const message = ref('点击任意图片打开预览')
const images = [
{ name: 'Avue 标志', thumbUrl: '/images/logo.png', url: '/images/logo.png', type: 'img' },
{ name: '布局示意', thumbUrl: '/images/grid.png', url: '/images/grid.png', type: 'img' }
]
function open(index) {
opened.value = true
preview(images, index, {
interval: 10000,
closeOnClickModal: true,
click: (item, current) => { message.value = '点击了第 ' + (current + 1) + ' 项:' + item.name },
beforeClose: (items, current) => {
opened.value = false
message.value = '已关闭,最后查看:' + items[current].name
}
})
}
</script>
<style scoped>
.image-preview-demo__images { display: flex; flex-wrap: wrap; gap: 16px; }
.image-preview-demo__images button { cursor: pointer; padding: 12px; background: white; border: 1px solid var(--el-border-color); border-radius: 8px; }
.image-preview-demo__images img { display: block; width: 150px; height: 110px; object-fit: contain; }
</style>