在el-image组件的预览中添加打印功能(自定义功能)

思路:给图片添加点击事件,通过js获取预览的工具栏,在工具栏中添加自定义按钮及事件

1、html 中 image标签

<el-image

style="width: 139px;

height: 89px"

:src="fileUrl"

:preview-src-list="[fileUrl]"

@click="handleImageClick(fileUrl)" //添加点击事件

/>

2、再点击事件中,通过js操作dom,添加自定义按钮事件

handleImageClick(fileUrl) {

this.$nextTick(() => {

const viewer = document.querySelector('.el-image-viewer__wrapper') //工具栏的dom

if (viewer) {

// 防止重复添加

if (!document.querySelector('.custom-print-btns')) {

const btn = document.createElement('button') //创建按钮

btn.className = 'custom-print-btns' //按需设计样式

btn.innerHTML = '<i class="el-icon-printer"></i>'

btn.onclick = () => this.printImage(fileUrl) //点击按钮触发事件

const btnGroup = viewer.querySelector('.el-image-viewer__actions__inner')

if (btnGroup) {

btnGroup.appendChild(btn) //在工具中添加按钮

}

}

}

})

},

3、添加后的图

4、打印事件

printImage(fileUrl) {

const printWindow = window.open('', '_blank')

printWindow.document.write(`

<html>

<head>

<title>打印图片</title>

<style>

body { text-align: center; margin: 0; padding: 0; }

img { max-width: 100%; max-height: 100vh; margin: auto; }

</style>

</head>

<body>

<img src="${fileUrl}" />

<script>

window.onload = function() {

setTimeout(function() {

window.print();

window.close();

}, 200);

}

<\/script>

</body>

</html>

`)

printWindow.document.close()

},

5、结果图

相关推荐
肖。35487870941 天前
html选择页最简模板源码,用于集合很多网页!游戏大全数字导航页面,数字选择页制作灵感,已经压缩到最小,现代,讲解。
android·java·javascript·css·html
qq_177767371 天前
React Native鸿蒙跨平台完成动漫应用实现本地数据持久化、收藏管理、观看历史记录、标签页切换
javascript·react native·react.js·ecmascript·harmonyos
木子啊1 天前
Uni-app跨页面通信三剑客
前端·uni-app·传参
tao3556671 天前
【用AI学前端】准备工作
前端·人工智能
利刃大大1 天前
【Vue】自定义指令directives && 指令钩子 && IntersectionObserver
前端·javascript·vue.js
共享家95271 天前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
Halo_tjn1 天前
基于封装的专项 知识点
java·前端·python·算法
摘星编程1 天前
OpenHarmony环境下React Native:自定义useTruncate文本截断
javascript·react native·react.js
Duang007_1 天前
【LeetCodeHot100 超详细Agent启发版本】字母异位词分组 (Group Anagrams)
开发语言·javascript·人工智能·python
有来技术1 天前
Spring Boot 4 + Vue3 企业级多租户 SaaS:从共享 Schema 架构到商业化套餐设计
java·vue.js·spring boot·后端