图片拖拽以及放大缩小
<template>
<a-button style="margin-left: 3px" size="small" type="primary" @click="magnifyImg">放大
</a-button>
<a-button style="margin-left: 3px" size="small" type="primary" @click="shrinkImg">缩小
</a-button>
<div :style="styleObjectWrapper" ref="imgWrapper" class="imgWrapper" @mouseover="onMouseOver">
<div
class="movableItem"
:style="movableItemStyle"
ref="bigImage"
@mousedown="drag($event,1)"
@contextmenu.prevent
>
<img
:src="src"
ref="mainImg"
oncontextmenu="return false;"
onselectstart="return false;"
draggable="false"
alt
:style="{
width:''+imgScaleWidth+'px',
height:''+imgScaleHeight+'px',
}"
/>
</div>
</div
</template>
<script>
export default {
data () {
return {
positionX: 0,
positionY: 0,
imgWidth: 0,
imgHeight: 0,
imgScaleWidth: 0,
imgScaleHeight: 0,
multiples: 1,
src: '',
movableItemStyle: {
position: 'absolute',
top: '0px',
left: '0px'
},
styleObjectWrapper: {
overflow: 'hidden',
position: 'relative',
backgroundColor: '#F2F2F2',
width: '100%',
height: document.body.clientHeight - 300 + 'px'
}
}
},
methods: {
magnifyImg () {
if (this.multiples >= 2) {
return
}
this.multiples += 0.2
this.imgScaleWidth = this.imgWidth * this.multiples
this.imgScaleHeight = this.imgHeight * this.multiples
this.$refs.bigImage.style.top = (this.positionY * this.multiples) + 'px'
this.$refs.bigImage.style.left = (this.positionX * this.multiples) + 'px'
},
shrinkImg () {
if (this.multiples <= 0.3) {
return
}
this.multiples -= 0.2
this.imgScaleWidth = this.imgWidth * this.multiples
this.imgScaleHeight = this.imgHeight * this.multiples
this.$refs.bigImage.style.top = (this.positionY * this.multiples) + 'px'
this.$refs.bigImage.style.left = (this.positionX * this.multiples) + 'px'
},
downloadByBlob (url) {
const vm = this
const image = new Image()
image.src = url
image.onload = () => {
vm.imgWidth = image.width
vm.imgHeight = image.height
vm.imgScaleWidth = image.width
vm.imgScaleHeight = image.height
vm.src = url
}
},
drag (ev) {
var _this = this
console.log('点击图片')
var nn6 = document.getElementById && !document.all
var isdrag = false
var y, x
var nTY, nTX
var oDragObj
function moveMouse (e) {
if (isdrag) {
const eY = nn6 ? e.clientY : event.clientY
const eX = nn6 ? e.clientX : event.clientX
oDragObj.style.top = (nTY + eY - y) + 'px'
oDragObj.style.left = (nTX + eX - x) + 'px'
_this.positionY = (nTY + eY - y) / _this.multiples
_this.positionX = (nTX + eX - x) / _this.multiples
return false
}
}
function initDrag (e) {
console.log('点击图片initDrag')
var oDragHandle = nn6 ? e.target : event.srcElement
var topElement = 'HTML'
while (
oDragHandle.tagName !== topElement &&
oDragHandle.className !== 'movableItem'
) {
oDragHandle = nn6
? oDragHandle.parentNode
: oDragHandle.parentElement
}
if (oDragHandle.className === 'movableItem') {
isdrag = true
oDragObj = oDragHandle
const width = _this.$refs.imgWrapper.offsetWidth
const height = _this.$refs.imgWrapper.offsetHeight
if (oDragObj.style.top === '') {
nTY = parseInt(50 * height / 100 + 0)
nTX = parseInt(50 * width / 100 + 0)
} else {
nTY = parseInt(oDragObj.style.top)
nTX = parseInt(oDragObj.style.left)
}
y = nn6 ? e.clientY : event.clientY
x = nn6 ? e.clientX : event.clientX
oDragObj.style.cursor = 'move'
document.onmousemove = moveMouse
return false
}
}
document.onmousemove = initDrag
document.onmouseup = function (e) {
isdrag = false
document.onmousemove = null
document.onmouseup = null
var oDragHandle = nn6 ? e.target : event.srcElement
var topElement = 'HTML'
while (
oDragHandle.tagName !== topElement &&
oDragHandle.className !== 'movableItem'
) {
oDragHandle = nn6
? oDragHandle.parentNode
: oDragHandle.parentElement
}
if (oDragHandle.className === 'movableItem') {
oDragObj = oDragHandle
oDragObj.style.cursor = 'Default'
}
}
ev = event || window.event
window.event ? (window.event.cancelBubble = true) : ev.stopPropagation()
},
}
}
</script>