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

news/2025/2/24 19:45:00

效果图

在这里插入图片描述

需求

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

下载

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>javascript">
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>javascript">
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.niftyadmin.cn/n/5864758.html

相关文章

spring中关于Bean的复习(IOC和DI)

文章目录 1.spring程序开发步骤1.1 导入spring开发的基本包坐标1.2 编写Dao接口和实现类1.3 创建spring核心配置文件1.4 在spring配置文件中配置UserDaoImpl1.5 使用Spring的Api获得Bean实例 2. Bean实例化的三种方式2.1 无参构造方法实例化2.2 工厂静态方法实例化2.3 工厂实例…

Java 的 HttpClient 中使用 POST 请求传递参数

在 Java 的 HttpClient 中&#xff0c;如果使用 POST 请求传递参数&#xff0c;有两种常见方式&#xff1a; 通过请求体传递&#xff08;通常是 JSON 或 XML 格式&#xff0c;适用于 RPC&#xff09;。通过表单参数传递&#xff08;类似于 HTML 表单提交&#xff0c;使用键值对…

linux下软件安装、查找、卸载

目录 常见安装方式有三种&#xff1a; 1.源码安装。 2.rpm安装方式。 3.yum/apt工具级别安装。 对于前两种安装方式&#xff0c;因为软件可能有依赖关系&#xff08;安装的软件依赖于某些库&#xff0c;而这些库又依赖于某些库&#xff0c;这些都需要手动安装&#xff09;…

超级详细Spring AI运用Ollama大模型

大模型工具Ollama 官网:https://ollama.com/ Ollama是一个用于部署和运行各种开源大模型的工具; 它能够帮助用户快速在本地运行各种大模型&#xff0c;极大地简化了大模型在本地运行的过程。用户通过执行几条命令就能在本地运行开源大模型&#xff0c;如Lama 2等; 综上&#x…

对接扣子双向流式 TTS Demo

Web端对接Demo <!DOCTYPE html> <html lang"zh-CN"><head><meta charset"UTF-8"><title>TTS 测试</title> </head><body><h1>TTS 测试页面</h1><textarea id"textInput" rows&…

LeetCode刷题---二分查找---454

四数相加 II 题目 给你四个整数数组 nums1、nums2、nums3 和 nums4 &#xff0c;数组长度都是 n &#xff0c;请你计算有多少个元组 (i, j, k, l) 能满足&#xff1a; 0 < i, j, k, l < nnums1[i] nums2[j] nums3[k] nums4[l] 0 示例 1&#xff1a; 输入&#xf…

【2025深度学习环境搭建-1】在Win11上用WSL2和Docker解锁GPU加速

建议有&#xff1a; 较新的win11电脑&#xff0c;GPU是nvidia一点点Linux基础一点点Docker基础 一、安装WSL2 【控制面板】》【程序】》【启用或关闭Windows功能】 打开三个功能&#xff1a;【Hyper-V】【Virtual Machine Platform】【适用于Linux的Windows子系统】 可能看…

【Python + STM32 实现外设控制的从0-1实例教程-适合新手】

一、环境搭建与固件烧录 1. 硬件准备 STM32开发板:推荐支持 MicroPython 的型号(如STM32F4 Discovery、NUCLEO-F411RE)。USB转TTL模块:用于串口通信(如CH340、CP2102)。外设模块:LED、温湿度传感器(如DHT11)等。2. 软件准备 MicroPython固件:从MicroPython官网下载对…