图片位置细节图,图片拖拽

图片拖拽,图片位置细节图

在这里插入图片描述

<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 {
    		/*
		      imgDetailWidth 大图的缩略图宽度
		      ratio:缩略图宽度比例
		      positionDetailX:缩略图展示部分X偏移量
		      positionDetailY: 缩略图展示部分X偏移量
		      isShowDiv:是否展示缩略图
		      imgWrapperWidth: 图片包裹元素宽度
		      imgWrapperHeight: 图片包裹元素高度
		      left:移动背景图展示部分左右偏移
		      top:移动背景图展示部分左右偏移
		      imgWidth: 图片宽度
		      imgHeight: 图片高度
		      imgShowWidth: 缩略图展示部分宽度
		      imgShowHeight: 缩略图展示高度
		      positionX: 背景图部分X偏移量
		      positionY: 背景图部分Y偏移量
		      positionDetailX:缩略图展示部分X偏移量
		      positionDetailY: 缩略图展示部分X偏移量
		      onMouseDownFlag: 背景图按下
		      onMouseOverFlag: 背景图抬起
		      onMouseOverImgFlag: 缩略图展示部分按下
		      onMouseDownImgFlag: 缩略图展示部分抬起
		      styleDetailImg:缩略图样式
		       */
		      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: '',
		      // showImgOffsetX: '',
		      // showImgOffsetY: '',
		      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'
		        // const imgWrapper = vm.$refs.imgWrapper
		
		        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.width = vm.computedWidth + 'px'
		        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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值