Skip to content

fox-preview-image 图片预览

安装

bash
npm i fox-preview-image #vue2.x 请使用 @1.x 版本

使用

js
// main.js
import { createApp } from "vue";
import App from "./App.vue";

import foxPreviewImage from "fox-preview-image"; // 引入
import "fox-preview-image/lib/style.css";

const app = createApp(App);
// ...

app.use(foxPreviewImage); // 注册组件
html
// App.vue
<script setup>
    import { ref } from "vue";
    const visible = ref(false);
    const visible = ref("");
</script>
<template>
    <for-preview-image v-model="visible" :src="src"></for-preview-image>
</template>

Attrs

属性说明参数类型默认值
src预览图片的地址string/Array<string>
model-value/v-model是否显示预览窗口booleanfalse
initial-index默认显示图片的下标(有多张图片时可以设置)number0
z-index预览窗口的层级number9000
show-toolbar是否显示底部 toolbarbooleantrue
enable-teleport启用 teleport 功能booleanfalse
append-to控制弹窗的插入位置HtmlElement/string-
layout工具栏的布局stringzoomOut,zoomIn,scale,position,rotateLeft,rotateRight,download

示例

单张图片

当图片只有一张时,不显示左右切换按钮

点击查看预览界面-没有切换按钮
点击查看示例代码
vue
<template>
    <ivy-button type="primary" @click="showModuleA"
        >点击查看预览界面</ivy-button
    >
    <fox-preview-image v-model="moduleA.visible" :src="moduleA.src" />
</template>
<script setup>
import { reactive } from "vue";
import imgA from "/images/preview-image-1.jpg";

const moduleA = reactive({
    visible: false,
    src: [imgA],
});

const showModuleA = () => {
    moduleA.visible = true;
};
</script>

多张图片

通过 initial-index 属性设置默认显示第二张图片

点击查看预览界面
点击查看示例代码
vue
<template>
    <ivy-button type="primary" @click="showModuleB"
        >点击查看预览界面</ivy-button
    >
    <fox-preview-image
        v-model="moduleB.visible"
        :src="moduleB.src"
        :initial-index="1" />
</template>
<script setup>
import { reactive } from "vue";
import imgA from "/images/preview-image-1.jpg";
import imgB from "/images/preview-image-2.png";
import imgC from "/images/preview-image-3.png";

const moduleB = reactive({
    visible: false,
    src: [imgA],
});

const showModuleB = () => {
    moduleC.visible = true;
};
</script>

自定义底部工具栏

通过 layout 属性设置底部要显示的工具和顺序

点击查看预览界面
点击查看示例代码
vue
<template>
    <ivy-button type="primary" @click="showModuleC"
        >点击查看预览界面</ivy-button
    >
    <fox-preview-image
        v-model="moduleC.visible"
        :src="moduleC.src"
        layout="zoomIn, zoomOut, position, scale" />
</template>
<script setup>
import { reactive } from "vue";
import imgA from "/images/preview-image-1.jpg";

const moduleC = reactive({
    visible: false,
    src: [imgA],
});

const showModuleC = () => {
    moduleC.visible = true;
};
</script>

和 elementPlus 公用 ZIndex

element-plus 导出了 useZIndex hook

vue
<script setup>
import { ref } from "vue";
import { useZIndex } from "element";
const { nextZIndex } = useZIndex();

const zIndex = ref(nextZIndex());
const openFoxPreviewImage = () => {
    // ...
    zIndex.value = nextZIndex();
};
</>
<template>
    <fox-preview-image :z-index="zIndex"></fox-preview-image>
</template>

MIT Licensed