图片拖拽,图片位置细节图
<template>
<div>
<div :style="styleObjectWrapper" ref="imgWrapper" class="imgWrapper" @mouseover="onMouseOver">
<div
:style="styleObjectImg"
@mousedown="onMouseDown"
@mousemove="onMouseMove($event)"
@mouseup="onMouseUp"
@mouseout="onMouseOut"
@dragstart="onDragStart"
@mouseover="onMouseOver"
@contextmenu.prevent
>
</div>
</div>
<div class="detailImg" :style="styleDetailImg" ref="detailImg">
<div
class="showImg"
ref="showImg"
@mousemove="onMouseMoveShowImg($event)"
@mousedown="onMouseDownImg"
@mouseup="onMouseUpImg"
@mouseout="onMouseOutImg"
@dragstart="onDragStartImg"
@mouseover="onMouseOverImg"
@contextmenu.prevent
:style="{
'width': imgShowWidth+'px',
'height':imgShowHeight+'px',
'border':'1px solid red',
'position':'absolute',
'top':top+'px',
'left':left+'px'
}">
</div>
</div>
</div>
</template>
<script>
export default {
data () {
return {
doMove: false,
imgDetailWidth: 0,
ratio: 0,
screenWidth: document.body.clientWidth,
screenHeight: document.body.clientHeight,
screenSize: 0,
isShowDiv: true,
computedHeight: '',
computedWidth: '',
imgWrapperWidth: '',
imgWrapperHeight: '',
detailImgWidth: '',
detailImgHeight: '',
showImgWidth: '',
showImgHeight: '',
left: 0,
top: 0,
imgWidth: 0,
imgHeight: 0,
imgShowWidth: 0,
imgShowHeight: 0,
positionX: 0,
positionY: 0,
positionDetailX: 0,
positionDetailY: 0,
onMouseDownFlag: false,
onMouseOverFlag: false,
onMouseOverImgFlag: false,
onMouseDownImgFlag: false,
styleObjectWrapper: {
overflow: 'hidden',
position: 'relative',
backgroundColor: '#F2F2F2',
width: '100%',
height: document.body.clientHeight - 300 + 'px'
},
styleObjectImg: {
backgroundImage: '',
backgroundRepeat: 'no-repeat',
backgroundPosition: '0px 0px',
cursor: 'auto',
width: '100%',
height: '100%',
transform: 'rotate(0deg)',
transformOrigin: 'center',
position: 'absolute'
},
styleDetailImg: {
backgroundImage: '',
backgroundRepeat: 'no-repeat',
backgroundPosition: 'center',
backgroundSize: '100% 100%',
backgroundColor: 'white',
zIndex: '20',
cursor: 'auto',
width: '0px',
height: '150px',
transform: 'rotate(0deg)',
transformOrigin: 'center',
position: 'absolute',
bottom: 0,
left: 0,
border: '1px solid black'
},
}
},
methods: {
downloadByBlob (url) {
const vm = this
const image = new Image()
image.src = url
image.onload = () => {
vm.imgWidth = image.width
vm.imgHeight = image.height
this.imgWrapperWidth = this.$refs.imgWrapper.offsetWidth
this.imgWrapperHeight = this.$refs.imgWrapper.offsetHeight
this.showImgWidth = this.$refs.showImg.offsetWidth
this.showImgHeight = this.$refs.showImg.offsetHeight
this.ratio = 150 / this.imgWrapperHeight
this.imgDetailWidth = 150 * vm.imgWidth / vm.imgHeight
this.imgShowHeight = this.imgWrapperHeight * 150 / this.imgHeight
this.imgShowWidth = this.imgWrapperWidth * this.imgDetailWidth / this.imgWidth
this.styleDetailImg.width = this.imgDetailWidth + 'px'
if (image.height > vm.screenHeight) {
vm.computedHeight = vm.screenHeight - 200
} else if (image.height > 670) {
vm.computedHeight = image.height
} else {
vm.computedHeight = 670
}
vm.styleObjectImg.backgroundImage = 'url(' + url + ')'
vm.styleDetailImg.backgroundImage = 'url(' + url + ')'
vm.styleObjectImg.transform = 'rotate(0deg)'
vm.styleObjectImg.height = vm.computedHeight + 'px'
vm.styleObjectImg.left = '0px'
vm.styleObjectImg.top = '0px'
vm.styleObjectWrapper.height = vm.computedHeight + 'px'
vm.styleLeftDev.height = vm.computedHeight + 'px'
}
},
onMouseDown () {
this.onMouseDownFlag = true
},
onMouseDownImg () {
this.onMouseDownImgFlag = true
},
onMouseUp (event) {
this.onMouseDownFlag = false
},
onMouseUpImg (event) {
this.onMouseDownImgFlag = false
},
onMouseOver () {},
onMouseOverImg () {},
onMouseOut () {
this.onMouseOverFlag = false
this.onMouseDownFlag = false
},
onMouseOutImg () {
this.onMouseOverImgFlag = false
this.onMouseDownImgFlag = false
},
onMouseMove (e) {
if (this.onMouseDownFlag) {
const e = e || window.event
if (this.styleObjectImg.backgroundPosition) {
var rotate =
parseInt(this.styleObjectImg.transform.split('(')[1]) % 360
if (rotate < 0) {
rotate += 360
}
let x = 0
let y = 0
if (rotate % 360 === 0) {
x =
parseInt(this.styleObjectImg.backgroundPosition.split(' ')[0]) +
e.movementX
y =
parseInt(this.styleObjectImg.backgroundPosition.split(' ')[1]) +
e.movementY
} else if (rotate % 270 === 0) {
x =
parseInt(this.styleObjectImg.backgroundPosition.split(' ')[0]) -
e.movementY
y =
parseInt(this.styleObjectImg.backgroundPosition.split(' ')[1]) +
e.movementX
} else if (rotate % 180 === 0) {
x =
parseInt(this.styleObjectImg.backgroundPosition.split(' ')[0]) -
e.movementX
y =
parseInt(this.styleObjectImg.backgroundPosition.split(' ')[1]) -
e.movementY
} else if (rotate % 90 === 0) {
x =
parseInt(this.styleObjectImg.backgroundPosition.split(' ')[0]) +
e.movementY
y =
parseInt(this.styleObjectImg.backgroundPosition.split(' ')[1]) -
e.movementX
}
this.positionX = x
this.positionY = y
this.styleObjectImg.backgroundPosition = x + 'px ' + y + 'px'
this.$refs.showImg.style.left = 0 - this.positionX * this.ratio - this.imgShowWidth / 2 + 'px'
this.$refs.showImg.style.top = 0 - this.positionY * this.ratio - this.imgShowHeight / 2 + 'px'
if (parseInt(this.$refs.showImg.style.left) < 0) {
this.$refs.showImg.style.left = 0 + 'px'
}
if (parseInt(this.$refs.showImg.style.top) < 0) {
this.$refs.showImg.style.top = 0 + 'px'
}
} else {
this.styleObjectImg.backgroundPosition = (this.computedWidth - this.imgWidth) / 2 + 'px ' + (this.computedHeight - this.imgHeight) / 2 + 'px'
}
}
},
onMouseMoveShowImg (e) {
if (this.onMouseDownImgFlag) {
const event = e || window.event
var rotate = parseInt(this.styleDetailImg.transform.split('(')[1]) % 360
if (rotate < 0) {
rotate += 360
}
let x = 0
let y = 0
if (rotate % 360 === 0) {
x =
parseInt(this.$refs.showImg.style.left) +
event.movementX
y =
parseInt(this.$refs.showImg.style.top) +
event.movementY
} else if (rotate % 270 === 0) {
x =
parseInt(this.$refs.showImg.style.left) -
event.movementY
y =
parseInt(this.$refs.showImg.style.top) +
event.movementX
} else if (rotate % 180 === 0) {
x =
parseInt(this.$refs.showImg.style.left) -
event.movementX
y =
parseInt(this.$refs.showImg.style.top) -
event.movementY
} else if (rotate % 90 === 0) {
x =
parseInt(this.$refs.showImg.style.left) +
event.movementY
y =
parseInt(this.$refs.showImg.style.top) -
event.movementX
}
this.positionDetailX = x
this.positionDetailY = y
this.positionX = (0 - x - this.imgShowWidth / 2) / this.ratio
this.positionY = (0 - y - this.imgShowHeight / 2) / this.ratio
this.$refs.showImg.style.left = this.positionDetailX + 'px'
this.$refs.showImg.style.top = this.positionDetailY + 'px'
this.styleObjectImg.backgroundPosition = (0 - this.positionDetailX - this.imgShowWidth / 2) / this.ratio + 'px ' + (0 - this.positionDetailY - this.imgShowHeight / 2) / this.ratio + 'px'
}
},
onDragStart () {
return false
},
onDragStartImg () {
return false
},
}
}
</script>
注意调用downloadByBlob方法,url为图片url