当前位置: 首页 > article >正文

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

效果图

在这里插入图片描述

需求

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

下载

cnpm in viewerjs

状态管理+方法

stores/imgSeeStore.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
        }
    }
})

封装的组件

<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

<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>

http://www.kler.cn/a/561909.html

相关文章:

  • 短剧源码部署搭建小程序搭建IAA+IAP混合解锁模式
  • 上海创智学院(测试)算法笔试(ACM赛制)部分例题
  • k8s集群3主5从高可用架构(kubeadm方式安装k8s)
  • 广州4399游戏25届春招游戏策划管培生内推
  • Linux搜索查找类指令
  • 【透视图像目标检测(3)】透视3D目标的航向角、观察角(局部方向)和相对位置角辨析,观察角的单目投影验证
  • 爬虫基础知识
  • 解析Excel表表头
  • 鸿蒙NEXT开发-用户通知服务
  • 矩阵碰一碰发视频的后端源码技术,支持OEM
  • StepAudio:语音大模型
  • Java所有运算符理解
  • ldap放大 DDOS.c
  • LeetCodehot 力扣热题100 全排列
  • ue学习part2
  • LeetCode每日精进:622.设计循环队列
  • C#快速调用DeepSeek接口,winform接入DeepSeek查询资料 C#零门槛接入DeepSeek C#接入DeepSeek源代码下载
  • K8S能部署大数据集群吗?为什么?K8S的HPA功能可以作为大数据处理消息积压的问题(Kafka的分区)
  • R 语言科研绘图 --- 柱状图-汇总
  • react18自定义hook实现