【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>
相关推荐
小飞侠在吗19 小时前
vue props
前端·javascript·vue.js
NiNi_suanfa20 小时前
【Qt】Qt 批量修改同类对象
开发语言·c++·qt
小糖学代码20 小时前
LLM系列:1.python入门:3.布尔型对象
linux·开发语言·python
Data_agent20 小时前
1688获得1688店铺详情API,python请求示例
开发语言·爬虫·python
大怪v20 小时前
【Virtual World 03】上帝之手
前端·javascript
用户8417948145621 小时前
vxe-gantt 甘特图实现产品进度列表,自定义任务条样式和提示信息
vue.js
妖灵翎幺21 小时前
C++ 中的 :: 操作符详解(一切情况)
开发语言·c++·ide
Halo_tjn21 小时前
虚拟机相关实验概述
java·开发语言·windows·计算机
star _chen21 小时前
C++实现完美洗牌算法
开发语言·c++·算法
周杰伦fans21 小时前
pycharm之gitignore设置
开发语言·python·pycharm