【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>
相关推荐
cloues break.6 分钟前
C++进阶----多态
开发语言·c++
我不会编程55513 分钟前
Python Cookbook-6.10 保留对被绑定方法的引用且支持垃圾回收
开发语言·python
道剑剑非道27 分钟前
QT开发技术【qcustomplot 曲线与鼠标十字功能】
开发语言·qt·计算机外设
一纸忘忧27 分钟前
成立一周年!开源的本土化中文文档知识库
前端·javascript·github
刘婉晴41 分钟前
【环境配置】Mac电脑安装运行R语言教程 2025年
开发语言·macos·r语言
Despacito0o41 分钟前
C++核心编程:类与对象全面解析
开发语言·c++
Tiger Z42 分钟前
R 语言科研绘图第 43 期 --- 桑基图-冲击
开发语言·r语言·贴图
涵信1 小时前
第九节:性能优化高频题-首屏加载优化策略
前端·vue.js·性能优化
全栈师1 小时前
C#中分组循环的做法
开发语言·c#
FAREWELL000751 小时前
C#进阶学习(十六)C#中的迭代器
开发语言·学习·c#·迭代器模式·迭代器