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

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

相关文章:

  • 【时时三省】(C语言基础)结构化程序设计方法
  • 二:前端发送POST请求,后端获取数据
  • Java并发 ThreadLocal 原理(详解)
  • c++中,什么时候应该使用mutable关键字?
  • Bash Shell控制台终端命令合集
  • C语言番外篇(3)------------>break、continue
  • 论文笔记:Autonomy-of-Experts Model
  • watchEffect 里有响应式依赖时并没有自动追踪
  • C++关键字之mutable
  • Tesseract OCR:起源、发展与完整使用指南
  • 多线程篇学习面试
  • 请谈谈 Vue 中的 key 属性的重要性,如何确保列表项的唯一标识?
  • 设计模式Python版 中介者模式
  • Vue 3 + Vite 项目中配置代理解决开发环境中跨域请求问题
  • Linux系统管理与编程01:准备工作
  • vim 多个关键字高亮插件介绍
  • A. Jagged Swaps
  • mybatis从接口直接跳到xml的插件
  • 不同activity的mViewModel是复用同一个的还是每个activity都是创建新的ViewModel
  • DeepSeek各模型现有版本对比分析
  • 体坛联播|利物浦提前4轮夺冠,安切洛蒂已向皇马更衣室告别
  • 十四届全国人大常委会第十五次会议在京举行,审议民营经济促进法草案等
  • 西北大学党委副书记吕建荣调任西安财经大学党委书记
  • 最高法改判一起植物新品种侵权案:判赔逾5300万元破纪录
  • 一季度煤价持续下探,多家煤炭巨头营收下滑、净利润降约两成
  • 铜钴巨头洛阳钼业一季度净利润同比大增九成,最新宣布超30亿元收购黄金资产