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

vue3 + antd实现简单的图片点开可以缩小放大查看

通过ref传参src打开弹窗

点击图片放大


<imgv-if="getFileType(record[column.key]) === '图片'":src="`${defaultBaseURL + record[column.key]}`"class="img-style"@click="handleImgClick(`${defaultBaseURL + record[column.key]}`)"/>// 点击打开图片
function handleImgClick(imgUrl: string) {showImgRef.value.openModel(imgUrl);
}// 引用图片展示组件
<ModelShowImg ref="showImgRef" />

图片展示组件代码

<script lang="ts" setup>
import { ref } from 'vue';import { CloseOutlined } from '@ant-design/icons-vue';const open = ref(false);
const showImg = ref('');
const x = ref(0);
const y = ref(0);
const startx = ref(0);
const starty = ref(0);
const endx = ref(0);
const endy = ref(0);
const height = ref(800);
function closeImg() {showImg.value = '';open.value = false;x.value = 0;y.value = 0;startx.value = 0;starty.value = 0;endx.value = 0;endy.value = 0;height.value = 800;
}
function changeImg(e: any) {if (e.deltaY < 0) {height.value += 80;} else {if (height.value > 80) {height.value -= 80;}}
}
function mousedown(e: any) {startx.value = e.pageX;starty.value = e.pageY;document.addEventListener('mousemove', mousemove);document.querySelector('#imgId')?.addEventListener('mouseup', mouseup);
}
function mousemove(e: any) {x.value = e.pageX - startx.value + endx.value;y.value = e.pageY - starty.value + endy.value;
}
function mouseup() {// 解除绑定mousemovedocument.removeEventListener('mousemove', mousemove, false);endx.value = x.value;endy.value = y.value;
}
function openModel(imgUrl: string) {showImg.value = imgUrl;open.value = true;
}
// 暴露方法给父组件调用
defineExpose({ openModel });
</script>
<template><a-modalv-model:open="open":centered="true":closable="false":footer="null":mask-closable="false"width="100%"wrap-class-name="dialogModalBox"@cancel="closeImg"><imgv-if="showImg"id="imgId":src="showImg":style="{ transform: `translate(${x}px,${y}px)`, height: `${height}px` }"draggable="false"@mousedown="mousedown($event)"@mousewheel="changeImg($event)"/><CloseOutlined class="close" @click="closeImg" /></a-modal>
</template>
<style lang="less">
.dialogModalBox {.ant-modal .ant-modal-content {padding: 0;background-color: rgba(0, 0, 0, 0);display: flex;justify-content: center; /* 水平居中 */align-items: center; /* 垂直居中 */height: 100vh;}
}
.close {position: fixed;right: 30px;top: 30px;color: white;font-size: 32px;
}
</style>

效果:点击图片按下可以拖拽,滚轮可以放大缩小图片,右上角X按钮关闭

http://www.dtcms.com/a/336994.html

相关文章:

  • 浅谈 Python 正则表达式中的 groups()
  • Linux文件相关命令
  • Disbursement on Quarantine Policy(概率、逆元计算期望)
  • 寻北仪如何靠“小”征服地下世界?
  • AI需要防火墙,云计算需要重新构想
  • 20. 云计算-多租户
  • 猫头虎AI分享|一款智能量化交易系统:QuantCell,从数据收集到策略执行全流程自动化
  • C++ 多进程编程深度解析【C++进阶每日一学】
  • 人工智能技术爆发的时代,AI工具和大模型对行业的深刻变革及实际应用!
  • 面向R语言用户的Highcharts
  • DR200差速移动机器人的多功能感知系统与多场景应用
  • 【网络安全实验报告】实验二: 后门木马配置及分析
  • 【网络安全实验报告】实验三:古典加密算法的实现
  • 数字化转型:概念性名词浅谈(第四十一讲)
  • 如何在VS Code中使用Copilot与MCP服务器增强开发体验
  • ThinkPHP的安装运行和调试
  • 动态规划----3.打家劫舍
  • EasyExcel篇
  • C语言篇:猜数字游戏的实现教程
  • 20250818 割点 割边 点双总结
  • 一个基于纯前端技术实现的五子棋游戏,无需后端服务,直接在浏览器中运行。
  • 数据挖掘 3.5 支持向量机——边界和正则化
  • 二分查找例题
  • 从基础到架构的六层知识体系
  • 进阶向:人物关系三元组,解锁人物关系网络的钥匙
  • 如何新建一个自己的虚拟环境
  • 有向无环图(Directed Acyclic Graph, DAG)介绍(环检测、DFS法、Kahn算法、)
  • 【Langchain系列三】GraphGPT——LangChain+NebulaGraph+llm构建智能图数据库问答系统
  • 15.三数之和
  • InfluxDB 开发工具链:IDE 插件与调试技巧(二)