ImagePreview — 이미지 미리보기
이미지와 동영상을 전체 화면으로 미리 볼 수 있는 컴포넌트예요. 함수 호출 방식도 지원해서, 컴포넌트를 미리 배치하지 않고도 필요한 순간에 프리뷰를 띄울 수 있어요.
소개
이미지와 동영상의 전체 화면 미리보기를 지원하고, 함수 호출도 지원해요.
설치
import { createApp } from 'vue'
import { ImagePreview } from '@nutui/nutui'
const app = createApp()
app.use(ImagePreview)
기본 사용법
:::demo
<template>
<nut-image-preview :show="state.showPreview" :images="state.imgData" @close="hideFn" />
<nut-cell isLink title="Show preview" :showIcon="true" @click="showFn"></nut-cell>
</template>
<script setup>
import { reactive } from 'vue'
const state = reactive({
showPreview: false,
imgData: [
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/18629/34/3378/144318/5c263f64Ef0e2bff0/0d650e0aa2e852ee.jpg'
},
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/26597/30/4870/174583/5c35c5d2Ed55eedc6/50e27870c25e7a82.png'
},
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/9542/17/12873/201687/5c3c4362Ea9eb757d/60026b40a9d60d85.jpg'
},
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/30042/36/427/82951/5c3bfdabE3faf2f66/9adca782661c988c.jpg'
}
]
})
const showFn = () => {
state.showPreview = true
}
const hideFn = () => {
state.showPreview = false
}
</script>
:::
시작 번호 지정
:::demo
<template>
<nut-image-preview :show="state.showPreview" :images="state.imgData" :init-no="3" @close="hideFn" />
<nut-cell isLink title="With init no" :showIcon="true" @click="showFn"></nut-cell>
</template>
<script setup>
import { reactive } from 'vue'
const state = reactive({
showPreview: false,
imgData: [
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/18629/34/3378/144318/5c263f64Ef0e2bff0/0d650e0aa2e852ee.jpg'
},
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/26597/30/4870/174583/5c35c5d2Ed55eedc6/50e27870c25e7a82.png'
},
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/9542/17/12873/201687/5c3c4362Ea9eb757d/60026b40a9d60d85.jpg'
},
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/30042/36/427/82951/5c3bfdabE3faf2f66/9adca782661c988c.jpg'
}
]
})
const showFn = () => {
state.showPreview = true
}
const hideFn = () => {
state.showPreview = false
}
</script>
:::
페이지네이션 표시
:::demo
<template>
<nut-image-preview
:show="state.showPreview"
:images="state.imgData"
:pagination-visible="true"
pagination-color="red"
@close="hideFn"
/>
<nut-cell isLink title="With pagination" :showIcon="true" @click="showFn"></nut-cell>
</template>
<script setup>
import { reactive } from 'vue'
const state = reactive({
showPreview: false,
imgData: [
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/18629/34/3378/144318/5c263f64Ef0e2bff0/0d650e0aa2e852ee.jpg'
},
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/26597/30/4870/174583/5c35c5d2Ed55eedc6/50e27870c25e7a82.png'
},
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/9542/17/12873/201687/5c3c4362Ea9eb757d/60026b40a9d60d85.jpg'
},
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/30042/36/427/82951/5c3bfdabE3faf2f66/9adca782661c988c.jpg'
}
]
})
const showFn = () => {
state.showPreview = true
}
const hideFn = () => {
state.showPreview = false
}
</script>
:::
동영상 포함
:::demo
<template>
<nut-image-preview :show="state.showPreview" :images="state.imgData" :videos="state.videoData" @close="hideFn" />
<nut-cell isLink title="With videos" :showIcon="true" @click="showFn"></nut-cell>
</template>
<script setup>
import { reactive } from 'vue'
const state = reactive({
showPreview: false,
imgData: [
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/18629/34/3378/144318/5c263f64Ef0e2bff0/0d650e0aa2e852ee.jpg'
},
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/26597/30/4870/174583/5c35c5d2Ed55eedc6/50e27870c25e7a82.png'
},
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/9542/17/12873/201687/5c3c4362Ea9eb757d/60026b40a9d60d85.jpg'
},
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/30042/36/427/82951/5c3bfdabE3faf2f66/9adca782661c988c.jpg'
}
],
videoData: [
{
source: {
src: 'https://storage.jd.com/about/big-final.mp4?Expires=3730193075&AccessKey=3LoYX1dQWa6ZXzQl&Signature=ViMFjz%2BOkBxS%2FY1rjtUVqbopbJI%3D',
type: 'video/mp4'
},
options: {
muted: true,
controls: true
}
},
{
source: {
src: 'https://storage.jd.com/about/big-final.mp4?Expires=3730193075&AccessKey=3LoYX1dQWa6ZXzQl&Signature=ViMFjz%2BOkBxS%2FY1rjtUVqbopbJI%3D',
type: 'video/mp4'
},
options: {
muted: true,
controls: true
}
}
]
})
const showFn = () => {
state.showPreview = true
}
const hideFn = () => {
state.showPreview = false
}
</script>
:::
함수 호출
함수 호출은 taro에서는 지원하지 않아요.
:::demo
<template>
<nut-cell isLink title="Functional call" :showIcon="true" @click="fnShow"></nut-cell>
</template>
<script setup>
import { showImagePreview } from '@nutui/nutui'
import '@nutui/nutui/dist/packages/imagepreview/style'
import { reactive } from 'vue'
const state = reactive({
imgData: [
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/18629/34/3378/144318/5c263f64Ef0e2bff0/0d650e0aa2e852ee.jpg'
},
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/26597/30/4870/174583/5c35c5d2Ed55eedc6/50e27870c25e7a82.png'
},
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/9542/17/12873/201687/5c3c4362Ea9eb757d/60026b40a9d60d85.jpg'
},
{
src: '//m.360buyimg.com/mobilecms/s750x366_jfs/t1/30042/36/427/82951/5c3bfdabE3faf2f66/9adca782661c988c.jpg'
}
]
})
const onClose = () => {
console.log('imagepreview closed')
}
const fnShow = () => {
showImagePreview({
show: true,
images: state.imgData,
onClose
})
}
</script>
:::
API
Props
| Attribute | Description | Type | Default |
|---|---|---|---|
| show | Whether to show preview | boolean | false |
| videos | Videos Array(Videos are before images, not support in taro) | VideoArray[] | [] |
| images | Images array | ImageArray[] | [] |
| autoplay | Autoplay time, zero means not autoplay | number | string | 3000 |
| init-no | Init no | number | 0 |
| pagination-visible | Whether to show pagination | boolean | false |
| pagination-color | Pagination color | string | #fff |
| content-close | Click image to exit preview | boolean | true |
| show-index | Whether to show index | boolean | true |
| closeable | Whether to show close icon | boolean | false |
| close-icon-position | Close icon position,can be set to top-left |
string | top-right |
| before-close | Callback function before close | (active: number) => boolean | Promise<boolean> |
- |
| max-zoom | Max zoomTaro isn't supported |
number | 3 |
| min-zoom | Min zoomTaro isn't supported |
number | 1/3 |
| is-Loop | Whether to loop | boolean | true |
ImageArray 데이터 구조
| Key | Description | Type |
|---|---|---|
| src | img url | string |
VideoArray 데이터 구조
| Key | Description | Type |
|---|---|---|
| source | Video url and type settings | object |
| options | Control video playback properties | object |
Events
| Event | Description | Arguments |
|---|---|---|
| close | Emitted when closing ImagePreview | - |
| change | Emitted when current image changed | index |
Slots
| Name | Description |
|---|---|
| closeIcon | Custom close icon |
더 알아보기 (Learn more)
이 문서는 NutUI v4 공식 영문 문서를 한국어로 옮긴 글이에요. 최신 API와 변경 사항은 원문에서 확인하는 게 가장 정확해요.
- 원문 문서: ImagePreview · doc.en-US.md
- NutUI 공식 문서: nutui.jd.com / GitHub 저장소: jd-opensource/nutui