【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>
相关推荐
Zfox_9 小时前
【LangGraph】持久化(Persistence)
开发语言·人工智能·redis·langchain·ai编程·langgraph
Cobyte9 小时前
11.响应式系统演进:深入剖析 computed 实现原理与性能优化实践(Vue3.3)
前端·javascript·vue.js
ZC跨境爬虫9 小时前
跟着MDN学HTML_day_46:(HTMLCollection与NodeList)
前端·javascript·ui·html·音视频
blxr_9 小时前
MySql锁机制
java·开发语言
极梦网络无忧9 小时前
我开源了一个 Vue 3 动态表单组件库 —— real-vue3-easy-form
前端·vue.js·开源
计算机安禾9 小时前
【c++面向对象编程】第13篇:继承(三):同名隐藏与作用域覆盖
开发语言·c++·iphone
ch.ju9 小时前
Java Programming Chapter 3——Dynamic acquisition of array
java·开发语言
MXN_小南学前端9 小时前
Vue + Quill:富文本的添加、传输、展示逻辑,以及 csReplyQuill 组件封装
前端·vue.js
TechWayfarer9 小时前
AI的幻觉谁来买单?智能体时代的数据溯源与鉴权
开发语言·python·安全·ai
Str_Null9 小时前
Python 自动线性化 HTML/MD 表格的工程实践(一个读取表格并且提供输出的工具)
开发语言·python·html