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

vue2使用v-viewer图片预览:打开页面自动预览,禁止关闭预览,解决在微信浏览器的页面点击事件老是触发预览初始化的问题

1、安装:

npm install v-viewer viewerjs

2、在 main.js 中全局注册:

import Viewer from 'v-viewer';
import 'viewerjs/dist/viewer.css';
Vue.use(Viewer );
//配置项(可选,根据需求调整)
// Vue.use(Viewer, {
//   defaultOptions: {
//     zIndex: 9999, // 预览弹窗层级
//     // toolbar: true, // 是否显示工具栏(缩放、旋转等按钮)
//     toolbar: false, // 是否显示工具栏(缩放、旋转等按钮)
//     navbar: true, // 可选:同时隐藏底部缩略图导航
//     title: false, // 可选:隐藏标题栏
//     clickToClose: false, // 禁止点击阴影处关闭预览
//     zoomRatio: 0.1, // 每次缩放的比例
//     minZoomRatio: 0.1, // 最小缩放比例
//     maxZoomRatio: 10, // 最大缩放比例
//     toggleOnDblclick: false, // 双击是否切换缩放状态
//     // 更多配置参考:https://github.com/fengyuanchen/viewerjs#options
//   }
// });

3、使用:解决在微信浏览器的页面点击事件老是触发预览初始化的问题

文档:https://mirari.cc/posts/v-viewer
在这里插入图片描述

<template><div id="PDA_PP"><div class="navbar" @click.stop><i class="el-icon-arrow-left"></i><span>用心软件</span><i class="el-icon-more" @click="bottomDrawer"></i></div><div class="viewer" ref="imgContainer" v-viewer.static="viewerOptions"><img class="image" v-for="(item, index) in srcList" :key="index" :src="item"></div><el-drawer :with-header="false" :visible.sync="drawer" direction="btt" custom-class="btt-custom-drawer" :before-close="handleClose"><el-button plain class="el-drawer-item">设为默认</el-button><el-button plain class="el-drawer-item">删除图片</el-button><el-button plain class="el-drawer-item">手机拍照</el-button><el-button plain class="el-drawer-item">从手机相册选择</el-button><el-button plain class="el-drawer-item" @click="handleClose">取消</el-button></el-drawer></div>
</template>
<script>
export default {data() {return {imgdata: [{ id: 0, pjname: '大灯', pjcode: '33101', url: 'http://gzyxdoc.oss-cn-shenzhen.aliyuncs.com/doc/2025/04/d9d60e4f7bc8f5737c282a388e73eedd.png' },{ id: 1, pjname: '大灯', pjcode: '33101', url: 'http://gzyxdoc.oss-cn-shenzhen.aliyuncs.com/doc/2025/04/a99c2e5b3121012cddef0bbbcb1153ee.png' },{ id: 2, pjname: '大灯', pjcode: '33101', url: 'http://gzyxdoc.oss-cn-shenzhen.aliyuncs.com/doc/2025/06/ee3e8506953f3f3c9f4f014342e66071image/png' },],srcList: [],viewerOptions: {className: 'mobile-custom-viewer',zIndex: 999,inline: false,toolbar: false,navbar: true,title: false,button: false,backdrop: 'static',//禁止点击阴影关闭预览hide: function () {return true},},mIndex: 1,mItemInfo: {},drawer: false,}},created() {this.imgdata.forEach((item, index) => {this.srcList.push(item.url)});},mounted() {setTimeout(() => {const viewer = this.$refs.imgContainer.$viewer;console.log(viewer)if (viewer) viewer.show();this.update_mindex(this.mIndex);}, 100);},methods: {update_mindex(index) {const viewer = this.$refs.imgContainer.$viewer;if (viewer) viewer.view(index);},getCurrentIndex() {const viewer = this.$refs.imgContainer.$viewer;if (viewer) {console.log('当前显示的是第', viewer.index, '张图片')this.mIndex = viewer.index;this.mItemInfo = this.imgdata[this.mIndex];console.log(this.mItemInfo)}},bottomDrawer() {this.drawer = true;this.getCurrentIndex()},handleClose() {this.drawer = false;},},
}
</script>
<style lang="">
html,
body {width: 100%;height: 100%;padding: 0;margin: 0;background: #000;
}
#PDA_PP {width: 100%;height: 100%;overflow: hidden;background: #000;
}
.navbar {color: #fff;display: flex;align-items: center;justify-content: space-between;height: 50px;font-size: 18px;border-bottom: 1px solid #5c5c5c;position: relative;z-index: 1002;background: #000;
}
.navbar > i {padding: 10px 15px;
}
.viewer {display: flex;line-height: 20px;padding: 10px;margin: 10px;border-radius: 8px;display: none;
}
.image {width: 80px;height: 80px;margin-right: 10px;
}.mobile-custom-viewer .viewer-navbar {background: #fff;border-top-left-radius: 20px;border-top-right-radius: 20px;
}.btt-custom-drawer {border-top-left-radius: 20px;border-top-right-radius: 20px;height: auto !important;
}
.btt-custom-drawer .el-drawer__body .el-drawer-item {display: block;width: 100%;font-size: 16px;color: #000;height: 50px;line-height: 50px;text-align: center;margin: 0;padding: 0;outline: 0;border: 0;border-top: 1px solid #f7f7f7;font-family: 微软雅黑;background: #fff;
}
.btt-custom-drawer .el-drawer__body .el-drawer-item:hover {background: #fff;
}
.btt-custom-drawer .el-drawer__body .el-drawer-item:disabled {color: #999;
}
.btt-custom-drawer .el-drawer__body .el-drawer-item:first-child {border-top: 0;
}
.btt-custom-drawer .el-drawer__body .el-drawer-item:last-child {border-width: 4px;
}
</style>

4、效果图:
在这里插入图片描述

http://www.lryc.cn/news/595420.html

相关文章:

  • Linux 721 创建实现镜像的逻辑卷
  • 网络数据分层封装与解封过程的详细说明
  • 讯飞输入法3.0.1742功能简介
  • AI Agent开发学习系列 - langchain之LCEL(3):Prompt+LLM
  • 20250721
  • 【React】npm install报错npm : 无法加载文件 D:\APP\nodejs\npm.ps1,因为在此系统上禁止运行脚本。
  • 2x2矩阵教程
  • kafka 日志索引 AbstractIndex
  • 前端包管理工具深度对比:npm、yarn、pnpm 全方位解析
  • maven下载地址以及setting.xml配置
  • 【科研绘图系列】R语言绘制棒棒图和哑铃图
  • Pytorch01:深度学习中的专业名词及基本介绍
  • k8s查看某个pod的svc
  • 【高等数学】第五章 定积分——第一节 定积分的概念与性质
  • PostgreSQL SysCache RelCache
  • OCR 身份识别:让身份信息录入场景更高效安全
  • 低代码/无代码平台如何重塑开发生态
  • 机器学习week3-分类、正则化
  • 在翻译语义相似度和会议摘要相似度评估任务中 ,分类任务 回归任务 生成任务区别
  • 141 个 LangChain4j Maven 组件分类解析、多场景实战攻略
  • Sklearn 机器学习 IRIS数据 理解分类报告
  • 从实践出发--探究C/C++空类的大小,真的是1吗?
  • bmsimilarity的打分 调试参数
  • 选择排序 冒泡排序
  • windows电脑给iOS手机安装ipa包的方法
  • 宝塔面板Nginx报错: IP+端口可以直接从访问,反向代理之后就504了 Gateway Time-out
  • Xilinx FPGA XCKU115‑2FLVA1517I AMD KintexUltraScale
  • 052_迭代器(Iterator / ListIterator)
  • The Survey of Few-shot Prompt Learning on Graph
  • Ubuntu 22.04编译安装Nginx 1.28