vue实现图片旋转,放大缩小及拖动功能

<template>
	<div>
		<div class="uploadfile-showImg" @mousewheel="bbimg(this)">
			<div class="setting_box img-footer">
				<div @click="rotate()">旋转</div>
				<div @click="imgOut()">还原</div>
			</div>
			<img :src="imgUrl" alt="" class="imgclass" :style="test " @mousedown="imgMove" />
		</div>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				imgUrl: 'https://img1.baidu.com/it/u=3955310150,3274562539&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1702054800&t=89b775b8e5cf0ff132c2d7346ecf130e',
				deg: 0,
				test: '',
				zoomInShow: false,
				params: {
					zoom: 1,
					left: 0,
					top: 0,
					currentX: 0,
					currentY: 0,
					flag: false,
				}
			}
		},
		methods: {
			// 鼠标滚轮滚动实现图片放大缩小 
			bbimg(ev) {
				let e = ev || window.event
				this.params.zoom += e.wheelDelta / 1200
				if (this.params.zoom >= 0.2) {
					this.test = `transform:scale(${this.params.zoom}) rotateZ(${this.deg}deg);`
				} else {
					this.params.zoom = 0.2
					this.test = `transform:scale(${this.params.zoom}) rotateZ(${this.deg}deg);`
					return false
				}
			},
			// 实现图片拖拽
			imgMove(e) {
				let oImg = e.target
				let disX = e.clientX - oImg.offsetLeft
				let disY = e.clientY - oImg.offsetTop
				document.onmousemove = (e) => {
					e.preventDefault()
					let left = e.clientX - disX
					let top = e.clientY - disY
					this.test = this.test + `left: ${left}px;top: ${top}px;`
				}
				document.onmouseup = (e) => {
					document.onmousemove = null
					document.onmouseup = null
				}
			},
			// 旋转
			rotate() {
				this.deg += 90
				if (this.deg >= 360) {
					this.deg = 0
				}
				this.test = `transform: rotateZ(${this.deg}deg)`
			},
			// 还原
			imgOut() {
				this.deg = 0
				this.params = {
					zoom: 1,
					left: 0,
					top: 0,
					currentX: 0,
					currentY: 0,
					flag: false,
				}
				this.test = `transform: rotateZ(0deg)`
			},
		}
	}
</script>
<style>
	.uploadfile-showImg {
		width: 100%;
		height: 100vh;
		background-color: rgba(0, 0, 0, 1);
		position: fixed;
		z-index: 20;
		margin: 0 auto;
		top: 0;
		left: 0;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.setting_box {
		width: 100%;
		line-height: 30px;
		font-size: 16px;
		background-color: rgba(0, 0, 0, 0.3);
		position: absolute;
		bottom: 5%;
		z-index: 999;
		display: flex;
		align-content: center;
		justify-content: center;
	}

	.img-footer {
		display: flex;
		align-content: center;
		justify-content: center;
	}

	.img-footer div {
		background-color: #2696ee;
		padding: 10px 20px;
		color: #fff;
		margin: 0 10px;
		cursor: pointer;
		border-radius: 10px;
	}

	.imgclass {
		max-width: 95vw;
		max-height: 90vh;
		position: absolute;
		z-index: 22;
		margin-top: 40px;
		cursor: move;
	}
</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值