【vue2】实现长按图片保存功能
1 2 3 4 5 | <span : class = "[$style.wxCode, isShow && $style['show']]" @touchstart= "touchStart()" @touchend= "touchEnd()" ></span> |
ps.span元素为图片元素,我这里把图片设置为元素背景了,可以直接用img标签实现。以下是方法代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | showWxCode() { this .isShow = true ; }, touchEnd() { //手指离开 clearTimeout( this .timer); }, touchStart() { //手指触摸 clearTimeout( this .timer); //再次清空定时器,防止重复注册定时器 this .timer = setTimeout(() => { this .downloadImage(require( "./images/wx.jpg" ), "wx" ); }, 1000); }, downloadImage(imgsrc, name) { //下载图片地址和图片名 let image = new Image(); // 解决跨域 Canvas 污染问题 image.setAttribute( "crossOrigin" , "anonymous" ); image.onload = () => { let canvas = document.createElement( "canvas" ); canvas.width = image.width; canvas.height = image.height; let context = canvas.getContext( "2d" ); context.drawImage(image, 0, 0, image.width, image.height); let url = canvas.toDataURL( "image/png" ); //得到图片的base64编码数据 let a = document.createElement( "a" ); // 生成一个a元素 let event = new MouseEvent( "click" ); // 创建一个单击事件 a.download = name || "photo" ; // 设置图片名称 a.href = url; // 将生成的URL设置为a.href属性 a.dispatchEvent(event); // 触发a的单击事件 }; image.src = imgsrc; }, |
分类:
VUE从入门到放弃
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具