# Vue3-Image-Preview

Vue3的图片预览插件，内置图片预览组件、图片预览指令以及图片预览函数。使用起来灵活方便，可以满足大部分针对图片预览的需求。

## 安装 Install
```bash
npm install vue3-image-preview
```
or
```bash
yarn add vue3-image-preview
```

## 局部引入 Part Import
在要使用的.vue文件中，引入**vue3-image-preview**。
```ts
import {preview, vPreview, Vue3ImagePreview} from 'vue3-image-preview';
```
其中**preview**为图片预览函数，**vPreview**为图片预览指令（v-preview：需要在directives属性中注册），**Vue3ImagePreview**为图片预览组件（需要在components属性中注册）。

## 全局引入 Global Import
在入口文件中引入**vue3-image-preview**，并且使用.use()安装全局插件。
**v-preview**已注册为全局指令，**Vue3ImagePreview**已注册为全局组件。
```ts
import { createApp } from 'vue';
import App from './App.vue';
import preview from 'vue3-image-preview';

createApp(App).use(preview).mount('#app');
```
**preview**函数需要通过getCurrentInstance(Vue3的Composition Api)来引入。
```ts
const {proxy} = getCurrentInstance() as any;

proxy.preview({
    images: state.images1,
    index: state.index
});
```

## preview函数
### 示例
```vue
<button @click="preview1Event">预览1</button>
<button @click="preview2Event">预览2</button>
<button @click="preview3Event">预览3</button>
...
setup() {
    // 响应式数据
    const state: any = reactive<any>({
        image: 'https://www.43cx.com/d/file/20220704/e1eb727dc76e4dfc916048b32ab9d50c.jpg',
        images1: [
          'https://img.phb123.com/uploads/allimg/200615/60-2006151H317-52.jpg',
          'https://img.phb123.com/uploads/allimg/200615/60-2006151H318.jpg'
        ],
        images2: [
          {url: 'https://www.43cx.com/d/file/20220704/87fb4397c47fab8b9fb9496e5279591d.jpg'},
          {url: 'https://www.43cx.com/d/file/20220704/a958a8b2ae53bdf22d318ce0356d3ab8.jpg'}
        ],
        index: 0
    });
    
    const preview1Event = () => {
        preview({
          images: state.image
        });
    };
    
    const preview2Event = () => {
        preview({
          images: state.images1,
          index: state.index
        });
    };
    
    const preview3Event = () => {
        preview({
            images: state.images2,
            index: state.index
        });
    };
    
    return {
        ...toRefs(state),
        preview1Event,
        preview2Event,
        preview3Event
    };
}
```
![function.gif](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/b426f81cc51d4d3680ec7f7ead313a81~tplv-k3u1fbpfcp-watermark.image?)
### 参数
| 属性       | 描述                                                         | 类型   | 默认值   | 必填   |
| ---------- | ------------------------------------------------------------ | ------ | ------ | ------ |
| images | 当前预览的图片资源或图片资源数组。                        | String/Array | "" | 是 |
| index | 初始化预览图片的索引，仅当**images**类型为Array时可用。 | Number  | 0 | 否 |
| key | 仅当**images**为对象数组时，key为数组里对象的图片资源的属性名。 | String | "url" | 否 |
| zoom | 缩放事件的缩放速度。 | Number | 1.2 | 否 |

## v-preview指令
### 示例
默认，直接使用**v-preview**指令，会打开单个预览组件。
```html
<img v-preview src="https://img.phb123.com/uploads/allimg/200615/60-2006151H317-52.jpg"/>
```
![directive-default.gif](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3e957714062042028c94fae6481a1503~tplv-k3u1fbpfcp-watermark.image?)

分组，使用**v-preview:name**指令，指令值相同的都会在同一个预览组件中打开。
```html
<img v-preview:name="name1" src="https://img.phb123.com/uploads/allimg/200615/60-2006151H318-52.jpg"/>
<img v-preview:name="name1" src="https://img.phb123.com/uploads/allimg/200615/60-2006151H318-51.jpg"/>

<img v-preview:name="name2" src="https://img.phb123.com/uploads/allimg/200615/60-2006151H317-50.jpg"/>
<img v-preview:name="name2" src="https://img.phb123.com/uploads/allimg/200615/60-2006151H317-51.jpg"/>
```
![directive-group.gif](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f6b2e44a6e8f435bbbec4bbe763d8e63~tplv-k3u1fbpfcp-watermark.image?)

缩放，使用**v-preview:zoom**指令，会改变缩放事件的缩放速度。
```html
<img v-preview:zoom="zoom1" src="https://www.pipicats.com/uploadfile/2022/1214/202212141670990665241976.jpg"/>
```
![directive-scale.gif](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/59a82be6893847c4af3840179069ce0c~tplv-k3u1fbpfcp-watermark.image?)

配置，**v-preview**的值为属性包含"name"，"zoom"的对象。
```html
<img v-preview="config1" src="https://www.pipicats.com/uploadfile/2022/1214/202212141670990664685286.jpg"/>
<img v-preview="config2" src="https://www.pipicats.com/uploadfile/2022/1214/202212141670990665512352.jpg"/>
<img v-preview="config3" src="https://www.pipicats.com/uploadfile/2022/1214/202212141670990665810075.jpg"/>
<img v-preview="config4" src="https://www.pipicats.com/uploadfile/2022/1214/202212141670990665451069.jpg"/>
```
![directive-config.gif](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/04bf5e3c1dcd433594763b810a62ad89~tplv-k3u1fbpfcp-watermark.image?)

响应式数据
```vue
setup() {
  const {proxy} = getCurrentInstance() as any;
  // 响应式数据
  const state: any = reactive<any>({
    name1: 'name1',
    name2: 'name2',
    zoom1: 1.4,
    zoom2: 1.1,
    config1: {name: 'name3'},
    config2: {zoom: 1.1},
    config3: {name: 'name4', zoom: 1.4},
    config4: {name: 'name4', zoom: 1.1}
  });

  return {
    ...toRefs(state)
  };
}
```

| 参数       | 描述                                                         | 类型   | 默认值   | 必填   |
| ---------- | ------------------------------------------------------------ | ------ | ------ | ------ |
| name | 图片分组的名称。 | String | - | 否 |
| zoom | 缩放事件的缩放速度。 | Number | 1.2 | 否 |

## Vue3ImagePreview组件
所有包裹在**Vue3ImagePreview**组件里的img，都会在同一个预览组件中打开。
```html
<Vue3ImagePreview>
    <img src="https://img.phb123.com/uploads/allimg/200615/60-2006151H318-54.jpg">
    <img src="https://img.phb123.com/uploads/allimg/200615/60-2006151H318-50.jpg"/>
</Vue3ImagePreview>

<Vue3ImagePreview :zoom="1.1">
    <img src="https://www.pipicats.com/uploadfile/2022/1214/202212141670990665503106.jpg">
    <img src="https://www.pipicats.com/uploadfile/2022/1214/202212141670990666859115.jpg"/>
</Vue3ImagePreview>
```
![component.gif](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/84d53071ccb44471ac324ca4a5214740~tplv-k3u1fbpfcp-watermark.image?)

| 属性       | 描述                                                         | 类型   | 默认值   | 必填   |
| ---------- | ------------------------------------------------------------ | ------ | ------ | ------ |
| zoom | 缩放事件的缩放速度。 | Number | 1.2 | 否 |

- <a href="http://mengxianwei.site/vue3-image-preview" target="_blank">演示地址</a>
