【Viewer.js】vue3封装图片查看器

效果图

需求

  • 点击图片放大
  • 可关闭放大的 图片

下载

sh 复制代码
cnpm in viewerjs

状态管理+方法

stores/imgSeeStore.js

js 复制代码
import { defineStore } from 'pinia'
export const imgSeeStore = defineStore('imgSeeStore', {
    state: () => ({
        showImgSee: false,
        ImgUrl: '',
    }),
    getters: {
    },
    actions: {
        openImgShow(url) {
            this.ImgUrl = url
            this.showImgSee = true
        },
        resetSeeImg() {
            this.ImgUrl = ''
            this.showImgSee = false
        }
    }
})

封装的组件

html 复制代码
<template>
  <img ref="el" :src="ImgUrl" />
</template>

<script setup>
import "viewerjs/dist/viewer.css";
import Viewer from "viewerjs";
import { nextTick, onMounted, ref } from "vue";
import { storeToRefs } from "pinia";
import { globalStateStore } from "src/stores/globalState";

const useGlobalStateStore = globalStateStore(),
  { ImgUrl } = storeToRefs(useGlobalStateStore),
  { resetSeeImg } = useGlobalStateStore;

const el = ref();
onMounted(async () => {
  await nextTick();
  //   图片查看器关闭事件
  el.value.addEventListener("hide", () => resetSeeImg());

  new Viewer(el.value, {
    navbar: false,
    title: false,
  }).show();
});
</script>

使用

TestVue.vue

html 复制代码
<template>
  <!-- 这个是要点击查看的图片 -->
   <img 
       style="max-width: 200px" 
       :src="img"
       @click="openImgShow(img)"
     />

<img-see v-if="showImgSee" />
</template>

<script setup>
import { ref} from "vue";
import { storeToRefs } from "pinia";
import ImgSee from "src/components/ImgSee.vue";
import { imgSeeStore} from "src/stores/imgSeeStore";  

const img = ref('/public/test.jpg')
const useImgSeeStore= imgSeeStore(),
  { showImgSee } = storeToRefs(useImgSeeStore),
  { openImgShow } = useImgSeeStore;
</script>
相关推荐
AI视觉网奇2 分钟前
ue 虚幻引擎学习笔记
开发语言·虚幻引擎
幼儿园老大3 分钟前
告别代码屎山!UniApp + Vue3 自动化规范:ESLint 9+ 扁平化配置全指南
javascript·vue.js
ghie909016 分钟前
使用MATLAB的k-Wave工具箱进行超声CT成像
开发语言·matlab
catchadmin16 分钟前
PHP 8.6 新增 clamp() 函数
开发语言·php
杰克尼23 分钟前
蓝桥云课-5. 花灯调整【算法赛】
java·开发语言·算法
.小墨迹23 分钟前
C++学习之std::move 的用法与优缺点分析
linux·开发语言·c++·学习·算法·ubuntu
努力学习的小廉23 分钟前
【QT(五)】—— 常用控件(二)
开发语言·qt
wanghowie24 分钟前
01.02 Java基础篇|核心数据结构速查
java·开发语言·数据结构
daols8827 分钟前
vue 甘特图 vxe-gantt table 连接线的用法详解
vue.js·甘特图·vxe-table
Liu.77429 分钟前
vue3组件之间传输数据
前端·javascript·vue.js