位置: IT常识 - 正文

Vue2 实现图片的拖拽、缩放、旋转(vue设置图片)

编辑:rootadmin
Vue2 实现图片的拖拽、缩放、旋转

推荐整理分享Vue2 实现图片的拖拽、缩放、旋转(vue设置图片),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue实现图片编辑,vue使用图片,vue 显示图片,vue 显示图片,vue使用图片,vue如何引入图片,vue项目中图片放在哪里,vue项目中图片放在哪里,内容如对您有帮助,希望把文章链接给更多的朋友!

本文是基于vue2 实现图片的拖拽、旋转、鼠标滚动放大缩小等功能。

效果图分步骤实现

在这里看下 拖拽、旋转、缩放的几个方法

1.获取图片的实际宽高getImgSize(url) {return new Promise((resolve, reject) => {let imgObj = new Image();imgObj.src = url;imgObj.onload = () => {resolve({width: imgObj.width,height: imgObj.height,});};});}, 2.根据盒子的大小、图片的大小来计算 要显示多大的图片 async initImage() {if (!this.imageUrl) {return;}let { width, height } = await this.getImgSize(this.imageUrl);// 设置原始图片的大小let realWidth = width;let realHeight = height;// 获取高宽比例const whRatio = realWidth / realHeight;const hwRatio = realHeight / realWidth;//获取盒子的大小const boxW = this.$refs.maskBox.clientWidth;const boxH = this.$refs.maskBox.clientHeight;if (realWidth >= realHeight) {this.imgH = hwRatio * boxW;const nih = this.imgH;if (nih > boxH) {this.imgH = boxH;this.imgW = whRatio * boxH;} else {this.imgW = boxW;}this.top = (boxH - this.imgH) / 2;this.left = (boxW - this.imgW) / 2;} else {this.imgW = (boxH / realHeight) * realWidth;this.imgH = boxH;this.left = (boxW - this.imgW) / 2;}},

在这里主要是根据图片的宽高 ,以及盒子的大小来计算 盒子中展示多大的图片,将图片按照比例缩放后展示到盒子中。

3.拖拽图片

主要是监听@mousedown事件

onmousedownHandle(e) {const that = this;this.$refs.maskBox.onmousemove = function (el) {const ev = el || window.event; // 阻止默认事件ev.preventDefault();that.left += ev.movementX;that.top += ev.movementY;};this.$refs.maskBox.onmouseup = function () {// 鼠标抬起时将操作区域的鼠标按下和抬起事件置为null 并初始化that.$refs.maskBox.onmousemove = null;that.$refs.maskBox.onmouseup = null;};if (e.preventDefault) {e.preventDefault();} else {return false;}}, 4. 旋转图片handleRotate() {this.deg += 90;if (this.deg >= 360) {this.deg = 0;}this.size = 0;this.scale = `scale(1) rotateZ(${this.deg}deg)`;}, 5.监听鼠标的滚动,进行缩放图片Vue2 实现图片的拖拽、缩放、旋转(vue设置图片)

在 mounted() 中监听鼠标的滚动事件

mounted() {// 兼容火狐浏览器this.mousewheelevt = /Firefox/i.test(navigator.userAgent)? "DOMMouseScroll" : "mousewheel";// 为空间区域绑定鼠标滚轮事件 =》 处理函数是wheelHandle// 如果你监听了window的scroll或者touchmove事件,你应该把passive设置为true,这样滚动就会流畅很多this.$refs.maskBox.addEventListener(this.mousewheelevt, this.wheelHandle, {passive:true});},

处理鼠标的滚动事件

wheelHandle(e) {// 兼容性处理 => 火狐浏览器判断滚轮的方向是属性 detail,谷歌和ie浏览器判断滚轮滚动的方向是属性 wheelDeltaconst ev = e || window.event;// dir = -dir; // dir > 0 => 表示的滚轮是向上滚动,否则是向下滚动 => 范围 (-120 ~ 120)const dir = ev.detail ? ev.detail * -120 : ev.wheelDelta;//滚动的数值 / 2000 => 表示滚动的比例,用此比例作为图片缩放的比例this.imgScaleHandle(dir / 2000);},

放大缩小图片

/** * 图片的缩放 * zoom >0 放大 * zoom <0 缩小 */imgScaleHandle(zoom) {this.size += zoom;if (this.size < -0.5) {this.size = -0.5;}this.scale = `scale(${1 + this.size}) rotateZ(${this.deg}deg)`;},

页面销毁的时候 注意要取消鼠标的监听事件

beforeDestroy() {//取消监听this.$refs.maskBox.removeEventListener(this.mousewheelevt,this.wheelHandle,{passive:true});},

以上就是主要实现功能的方法

完整代码<template><div class="home"><div class="btn-area"><button @click="switchImgHandle(1)">竖图</button><button @click="switchImgHandle(2)">横图</button><button @click="handleRotate">旋转</button><button @click="imgScaleHandle(0.25)">放大</button><button @click="imgScaleHandle(-0.25)">缩小</button><button @click="handleReset">重置</button></div><div class="image-box" ref="maskBox" @mousedown="onmousedownHandle"><img :src="imageUrl" alt="" :style="{width: imgW + 'px', height: imgH + 'px',top: top + 'px', left: left + 'px', transform: scale, }" /></div></div></template><script>export default {name: "HomeView",data() {return {imageUrl: "",imageUrl1: require("@/assets/img1.jpg"),imageUrl2: require("@/assets/img2.jpg"),imgW: 0,imgW: 0,imgH: 0,deg: 0,top: 0,left: 0,scale: "scale(1)",size: 0,mousewheelevt: null,};},mounted() {this.imageUrl = this.imageUrl1;//初始化图片this.initImage();// 兼容火狐浏览器this.mousewheelevt = /Firefox/i.test(navigator.userAgent) ? "DOMMouseScroll" :"mousewheel";// 为空间区域绑定鼠标滚轮事件 =》 处理函数是wheelHandle// 如果你监听了window的scroll或者touchmove事件,你应该把passive设置为true,这样滚动就会流畅很多this.$refs.maskBox.addEventListener(this.mousewheelevt, this.wheelHandle, { passive:true});},beforeDestroy() {//取消监听this.$refs.maskBox.removeEventListener(this.mousewheelevt,this.wheelHandle,{passive:true});},methods: {/** * 切换图片 *flag: 1竖图 2 横图 */switchImgHandle(flag) {if (flag === 1) {this.imageUrl = this.imageUrl1;} else {this.imageUrl = this.imageUrl2;}this.handleReset();},/** * 获取图片的url * @param {string} url */getImgSize(url) {return new Promise((resolve, reject) => {let imgObj = new Image();imgObj.src = url;imgObj.onload = () => {resolve({width: imgObj.width,height: imgObj.height,});};});},/** * 初始化图片 */async initImage() {if (!this.imageUrl) {return;}let { width, height } = await this.getImgSize(this.imageUrl);// 设置原始图片的大小let realWidth = width;let realHeight = height;// 获取高宽比例const whRatio = realWidth / realHeight;const hwRatio = realHeight / realWidth;//获取盒子的大小const boxW = this.$refs.maskBox.clientWidth;const boxH = this.$refs.maskBox.clientHeight;if (realWidth >= realHeight) {this.imgH = hwRatio * boxW;const nih = this.imgH;if (nih > boxH) {this.imgH = boxH;this.imgW = whRatio * boxH;} else {this.imgW = boxW;}this.top = (boxH - this.imgH) / 2;this.left = (boxW - this.imgW) / 2;} else {this.imgW = (boxH / realHeight) * realWidth;this.imgH = boxH;this.left = (boxW - this.imgW) / 2;}},/** * 旋转 */handleRotate() {this.deg += 90;if (this.deg >= 360) {this.deg = 0;}this.size = 0;this.scale = `scale(1) rotateZ(${this.deg}deg)`;},/** * 图片的缩放 *zoom >0 放大 *zoom <0 缩小 */imgScaleHandle(zoom) {this.size += zoom;if (this.size < -0.5) {this.size = -0.5;}this.scale = `scale(${1 + this.size}) rotateZ(${this.deg}deg)`;},/** * 重置 */handleReset() {this.imgW = 0;this.imgH = 0;this.top = 0;this.left = 0;this.deg = 0;this.scale = "scale(1)";this.size = 0;this.initImage();},/** * 鼠标滚动 实现放大缩小 */wheelHandle(e) {const ev = e || window.event; // 兼容性处理 => 火狐浏览器判断滚轮的方向是属性 detail,谷歌和ie浏览器判断滚轮滚动的方向是属性 wheelDelta// dir = -dir; // dir > 0 => 表示的滚轮是向上滚动,否则是向下滚动 => 范围 (-120 ~ 120)const dir = ev.detail ? ev.detail * -120 : ev.wheelDelta;//滚动的数值 / 2000 => 表示滚动的比例,用此比例作为图片缩放的比例this.imgScaleHandle(dir / 2000);},/** * 处理图片拖动 */onmousedownHandle(e) {const that = this;this.$refs.maskBox.onmousemove = function (el) {const ev = el || window.event; // 阻止默认事件ev.preventDefault();that.left += ev.movementX;that.top += ev.movementY;};this.$refs.maskBox.onmouseup = function () {// 鼠标抬起时将操作区域的鼠标按下和抬起事件置为null 并初始化that.$refs.maskBox.onmousemove = null;that.$refs.maskBox.onmouseup = null;};if (e.preventDefault) {e.preventDefault();} else {return false;}},},};</script><style scoped>.home {width: 1000px;margin: 50px auto;}.btn-area {display: flex;justify-content: center;width: 100%;margin-bottom: 50px;}.btn-area button {width: 100px;height: 40px;font-size: 18px;margin-right: 10px;}.image-box {position: relative;margin: 0 auto;width: 1000px;height: 700px;border: 1px solid #333;overflow: hidden;}.image-box img {position: absolute;cursor: pointer;}</style> 最后

整理了一套《前端大厂面试宝典》,包含了HTML、CSS、JavaScript、HTTP、TCP协议、浏览器、VUE、React、数据结构和算法,一共201道面试题,并对每个问题作出了回答和解析。 有需要的小伙伴,可以点击文末卡片领取这份文档,无偿分享

部分文档展示:

文章篇幅有限,后面的内容就不一一展示了

有需要的小伙伴,可以点下方卡片免费领取

本文链接地址:https://www.jiuchutong.com/zhishi/289703.html 转载请保留说明!

上一篇:大熊雨林中一只柯莫德熊幼崽跟它的同胞挤在一起,加拿大 (© Ian McAllister/Offset)(大熊雨林中一只小熊)

下一篇:中科大ChatGPT学术镜像小白部署教程,全民都可以拥抱AI(中科大ustc-guest)

  • 预收货款需要缴纳消费税吗?缴纳消费税的时间应该是?
  • 无票收入怎么做账,要交税吗,填入增值税申报表
  • 增值税税率调整文件
  • 进销存工作流程
  • 社保缴费回单怎么查
  • 已交的增值税能计入费用吗
  • 金税盘地区编号怎么查
  • 证券公司清算交收
  • 企业所得税的应纳税所得额的扣除项目有哪些
  • 服务合同需要征税吗
  • 月末哪些科目需要手动结转为成本
  • 健身房开业前买的瑜伽垫怎么做账?
  • 基本生产车间领用周转材料会计分录
  • 含有商品编码的增值税普通发票如何开具?
  • 非居民企业减免的利息是否交增值税
  • 汽车修理费需要交印花税吗
  • 5年内亏损弥补账务
  • 资产负债表中资产总计和负债所有者权益不平等
  • 小规模税控盘服务费怎么填增值税申报表
  • 个体户需要对公户吗
  • 出口退税转内销还是转免税好
  • 公司购买理财的好处有哪些
  • 谨慎性原则的具体体现有哪些
  • 给员工买饮料
  • 税控盘服务费全额抵扣勾选在什么地方
  • php中strcmp函数
  • 股东分红个税怎么做账
  • 购买房地产和买房一样吗
  • 超市进货产品
  • php实现微信公众号分享
  • vue3怎么用
  • php foreach循环遍历数组
  • php tr td
  • 为什么税前利润加可抵扣暂时性差异
  • 盘盈的存货计入哪个科目
  • javascript获取字符串长度
  • 人工智能介绍200字
  • 使用ChatGPT进行AI对话
  • 管理费用包括哪些部门的工资
  • 会计人员报销差旅费应该给谁审核呢
  • 无形资产资本化和费用化的区别
  • 维修费应该计入什么科目
  • 准则规定的内容是
  • mysql union和join
  • 公司的融资租赁有哪些
  • 免税普票要交企业所得税吗
  • 如何把excel数据导入到word表格
  • 抵扣税款
  • 出口货物没有进项发票用什么平台申报
  • 银行承兑汇票贴现的账务处理
  • 收到投资款如何声明
  • 暂估入库有时间限制吗
  • 企业退休职工取暖费
  • 单位社保缴费收费标准
  • 协会会费怎么入账
  • 鉴证咨询服务费可以抵扣吗
  • 给别人的借款怎么记账
  • 填制会计凭证要求的是
  • 预收账款是什么资产
  • 年末未分配利润总额计算公式
  • mysql提高效率
  • sqlserver数据库事物日志已满
  • vista win
  • ubuntu设置关闭按钮在右侧
  • win7系统关机没反应
  • win1020h2无法重启
  • Win10 Mobile build 10586.338上手视频曝光
  • 当ie7不认!important之后 [布局的解决办法]
  • css如何控制图片位置
  • js实现简单的画图功能
  • Node.js中的什么模块是用于处理文件和目录的
  • windows批量添加文件后缀
  • angular scope
  • shell获取当前脚本的进程
  • unity只执行一次的方法
  • python调用python代码
  • jquery插件怎么写
  • python 脚本
  • h5实现微信分享
  • 贵州省 税务局
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设