vue组件实现淘宝京东商品放大镜效果

<template>
  <div style="display: flex; position: relative">
    <div
      class="box"
      :style="minImgBoxStyle"
      @mouseleave="mouseLeave"
      @mouseenter="mouseEnter"
      @mousemove="mousemove($event)"
    >
      <!--原始照片-小照片-->
      <img
        :style="minImgStyle"
        fit="contain"
        ref="minImg"
        :src="finalMinIMGsrc"
      />
      <!--探测块-->
      <div v-show="show" class="areaMark" :style="areaMarkStyle"></div>
    </div>
    <div class="box maxImgBox" :style="maxImgBoxStyle" v-show="show">
      <!--放大后的照片-->
      <img :style="maxImgStyle" fit="contain" :src="finalMaxIMGsrc" />
    </div>
  </div>
</template>
<script>
export default {
  name: 'zoom',
  props: {
    minIMGsrc: String,
    maxIMGsrc: String,
    scale: {
      type: Number,
      default: 2
    },
    width: {
      type: Number,
      default: 272
    },
    height: {
      type: Number,
      default: 272
    }
  },
  data () {
    return {
      show: false,
      finalMinIMGsrc: '',
      finalMaxIMGsrc: '',
      imgBoxWidth: '',
      imgBoxHeight: '',
      areaWidth: '',
      areaHeight: '',
      areaMarkStyle: {},
      minImgBoxStyle: {
        cursor: 'move'
      },
      minImgStyle: {},
      maxImgBoxStyle: {},
      maxImgStyle: {
        position: 'absolute'
      }
    }
  },
  watch: {
    minIMGsrc () {
      this.init()
    },
    maxIMGsrc () {
      this.init()
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    init () {
      this.imgBoxWidth = this.width
      this.imgBoxHeight = this.height
      this.$set(this.minImgStyle, 'width', this.imgBoxWidth + 'px')
      this.$set(this.minImgStyle, 'height', this.imgBoxHeight + 'px')
      this.$set(this.maxImgStyle, 'width', this.imgBoxWidth + 'px')
      this.$set(this.maxImgStyle, 'height', this.imgBoxHeight + 'px')
      this.$set(this.minImgBoxStyle, 'width', this.imgBoxWidth + 'px')
      this.$set(this.minImgBoxStyle, 'height', this.imgBoxHeight + 'px')
      this.$set(this.maxImgBoxStyle, 'width', this.imgBoxWidth + 'px')
      this.$set(this.maxImgBoxStyle, 'height', this.imgBoxHeight + 'px')
      this.$set(this.maxImgBoxStyle, 'left', this.imgBoxWidth + 'px')
      this.areaWidth = this.imgBoxWidth / this.scale
      this.areaHeight = this.imgBoxHeight / this.scale
      this.finalMinIMGsrc = this.minIMGsrc
      if (!this.maxIMGsrc) {
        this.finalMaxIMGsrc = this.minIMGsrc
      }
      this.$set(this.areaMarkStyle, 'width', this.areaWidth + 'px')
      this.$set(this.areaMarkStyle, 'height', this.areaHeight + 'px')
      this.$set(this.maxImgStyle, 'transform', 'scale(' + this.scale + ')')
    },
    mouseEnter () {
      this.show = true
    },
    mouseLeave () {
      this.show = false
    },
    mousemove (e) {
      // 获取鼠标相对于屏幕的坐标
      let mouseClientX = e.clientX
      let mouseClientY = e.clientY
      // 获取小照片相对于屏幕位置信息
      // getBoundingClientRect()用于获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置。
      let minImgPosition = this.$refs.minImg.getBoundingClientRect()
      let minImgX = minImgPosition.left
      let minImgY = minImgPosition.top
      // 计算出探测块相对于小图片的坐标
      let areaLeft = mouseClientX - minImgX - this.areaWidth / 2
      let areaTop = mouseClientY - minImgY - this.areaHeight / 2
      let minLeft = 0
      let maxLeft = this.imgBoxWidth - this.areaWidth
      let minTop = 0
      let maxTop = this.imgBoxHeight - this.areaHeight
      // 禁止探测块移出小图片
      if (areaLeft < minLeft) {
        areaLeft = minLeft
      }
      if (areaLeft > maxLeft) {
        areaLeft = maxLeft
      }
      if (areaTop < minTop) {
        areaTop = minTop
      }
      if (areaTop > maxTop) {
        areaTop = maxTop
      }
      // 更新探测块的坐标
      this.$set(this.areaMarkStyle, 'left', areaLeft + 'px')
      this.$set(this.areaMarkStyle, 'top', areaTop + 'px')
      // 更新放大后照片的坐标
      this.$set(
        this.maxImgStyle,
        'left',
        ((this.scale - 1) * this.imgBoxWidth) / 2 - areaLeft * this.scale + 'px'
      )
      this.$set(
        this.maxImgStyle,
        'top',
        ((this.scale - 1) * this.imgBoxHeight) / 2 - areaTop * this.scale + 'px'
      )
    }
  }
}
</script>
<style scoped>
.box {
  position: relative;
  overflow: hidden;
}
.areaMark {
  background: rgba(255, 255, 255, 0.4);
  position: absolute;
  top: 0px;
  left: 0px;
  cursor: move;
}
.maxImgBox {
  position: absolute;
  z-index: 999;
}
</style>
<img-zoom :scale=2.5 :minIMGsrc="xxx.jpg"></img-zoom>
import imgZoom from './imgZoom.vue'  // 没有大括号{}
components: {
  imgZoom
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值