【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>
相关推荐
大阳1239 分钟前
线程(基本概念和相关命令)
开发语言·数据结构·经验分享·算法·线程·学习经验
YA33317 分钟前
java基础(九)sql基础及索引
java·开发语言·sql
开发者小天23 分钟前
为什么 /deep/ 现在不推荐使用?
前端·javascript·node.js
奇树谦1 小时前
QT|windwos桌面端应用程序开发,当连接多个显示器的时候,如何获取屏幕编号?
开发语言·qt
weixin_307779131 小时前
VS Code配置MinGW64编译GNU 科学库 (GSL)
开发语言·c++·vscode·算法
froginwe112 小时前
HTML 框架:构建网页布局的基石
开发语言
Yn3122 小时前
在 Python 中使用 json 模块的完整指南
开发语言·python·json
一枚小小程序员哈2 小时前
基于Vue的个人博客网站的设计与实现/基于node.js的博客系统的设计与实现#express框架、vscode
vue.js·node.js·express
找不到工作的菜鸟2 小时前
Three.js三大组件:场景(Scene)、相机(Camera)、渲染器(Renderer)
前端·javascript·html
定栓2 小时前
vue3入门-v-model、ref和reactive讲解
前端·javascript·vue.js